Class: Object

Inherits:
BasicObject
Defined in:
lib/simple_delegator/object.rb

Class Method Summary collapse

Class Method Details

.delegate(method_name, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/simple_delegator/object.rb', line 2

def self.delegate(method_name, options = {})
  define_method(method_name) do
    to = options.fetch(:to, nil)

    target = if to.nil?
               self
             else
               if instance_variable_defined? "@#{to}"
                 instance_variable_get "@#{to}"
               else
                 self.send to
               end
             end

    new_method_name = options.fetch(:as, method_name)
    target.send(new_method_name)
  end
end