Skip to content

Instantly share code, notes, and snippets.

@dstrelau
Created June 17, 2009 20:38
Show Gist options
  • Save dstrelau/131491 to your computer and use it in GitHub Desktop.
Save dstrelau/131491 to your computer and use it in GitHub Desktop.
Configurable - make any object configurable
require 'ostruct'
module Configurable
def config
@config ||= OpenStruct.new
yield @config if block_given?
end
def respond_to?(sym)
@config.respond_to?(sym) || super(sym)
end
def method_missing(sym, *args, &block)
if @config.respond_to?(sym)
@config.send(sym, *args, &block)
else
super(sym, *args, &block)
end
end
end
[tmp] irb -r configurable
>> module Foo
>> extend Configurable
>> end
=> Foo
>> Foo.config do |config|
?> config.setting_a = 'a'
>> config.setting_b = 'b'
>> end
=> "b"
>> Foo.setting_a
=> "a"
>> Foo.setting_b
=> "b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment