Method: Module#set
- Defined in:
- lib/core/facets/module/set.rb
#set(option, value = self, &block) ⇒ Object
Sets an option to the given value. If the value is a proc, the proc will be called every time the option is accessed.
CREDIT: Blake Mizerany (Sinatra)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/core/facets/module/set.rb', line 10 def set(option, value=self, &block) raise ArgumentError if block && value != self value = block if block if value.kind_of?(Proc) if value.arity == 1 yield self else (class << self; self; end).module_eval do define_method(option, &value) define_method("#{option}?"){ !!__send__(option) } define_method("#{option}="){ |val| set(option, Proc.new{val}) } end end elsif value == self option.each{ |k,v| set(k, v) } elsif respond_to?("#{option}=") __send__("#{option}=", value) else set(option, Proc.new{value}) end self end |