Skip to content

Instantly share code, notes, and snippets.

@wobh
Created January 29, 2015 11:26
Show Gist options
  • Save wobh/6a149f4960bf621d8b96 to your computer and use it in GitHub Desktop.
Save wobh/6a149f4960bf621d8b96 to your computer and use it in GitHub Desktop.
Minimum configurable Ruby class
# Goals
# 1. Small class configurable with a block
# 2. puts settings in a ruby Struct
require 'Forwardable'
class Settable
extend Forwardable
def_delegator :@config, :foo
def_delegator :@config, :bar
def initialize(&settings)
@config = Struct.new(:foo, :bar).new
settings.call(@config) if settings
@config.freeze
end
end
# TODO: try with a different delegator
# FIXME: try not to expose `config' class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment