Class: Object

Inherits:
BasicObject
Defined in:
lib/selective_binding.rb

Instance Method Summary collapse

Instance Method Details

#selective_binding(*methods, &block) ⇒ Object

Creates a selective binding instance for self and returns the newly created binding:

selective_binding :attr1, :method1
#=> <#Binding>

Passing a hash as the last argument allows for setting custom attributes. These override any previously defined forwarded methods:

selective_binding :attr1 => "custom value"
#=> <#Binding>

Passing a block is also supported to set the default value for undefined attributes or methods:

selective_binding do
  "default value"
end
#=> <#Binding>


140
141
142
143
144
145
146
147
148
149
# File 'lib/selective_binding.rb', line 140

def selective_binding *methods, &block
  hash_attrib = methods.delete_at(-1) if Hash === methods.last

  binder = SelectiveBinding.new self

  binder.forward(*methods)       unless methods.empty?
  binder.import_hash hash_attrib if hash_attrib
  binder.set_default(&block)     if block_given?
  binder.get_binding
end