Created
June 17, 2009 20:38
-
-
Save dstrelau/131491 to your computer and use it in GitHub Desktop.
Configurable - make any object configurable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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