Module: Forwardable

Included in:
Date
Defined in:
opal/stdlib/forwardable.rb

Instance Method Summary collapse

Instance Method Details

#def_instance_delegator(accessor, method, ali = method) ⇒ Object Also known as: def_delegator



20
21
22
23
24
25
26
27
28
29
30
# File 'opal/stdlib/forwardable.rb', line 20

def def_instance_delegator(accessor, method, ali = method)
  if accessor.to_s.start_with? '@'
    define_method ali do |*args, &block|
      instance_variable_get(accessor).__send__(method, *args, &block)
    end
  else
    define_method ali do |*args, &block|
      __send__(accessor).__send__(method, *args, &block)
    end
  end
end

#def_instance_delegators(accessor, *methods) ⇒ Object Also known as: def_delegators



12
13
14
15
16
17
18
# File 'opal/stdlib/forwardable.rb', line 12

def def_instance_delegators(accessor, *methods)
  methods.each do |method|
    next if %w[__send__ __id__].include?(method)

    def_instance_delegator(accessor, method)
  end
end

#instance_delegate(hash) ⇒ Object Also known as: delegate



2
3
4
5
6
7
8
9
10
# File 'opal/stdlib/forwardable.rb', line 2

def instance_delegate(hash)
  hash.each do |methods, accessor|
    methods = [methods] unless methods.respond_to? :each

    methods.each do |method|
      def_instance_delegator(accessor, method)
    end
  end
end