dstrelau (owner)

Revisions

gist: 131491 Download_button fork
public
Description:
Configurable - make any object configurable
Public Clone URL: git://gist.github.com/131491.git
configurable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Text only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[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"