Module: Forwardable::Extended

Includes:
Forwardable
Defined in:
lib/forwardable/extended.rb,
lib/forwardable/extended/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#def_hash_delegator(hash, *methods, key: nil, bool: false, revbool: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/forwardable/extended.rb', line 12

def def_hash_delegator(hash, *methods, key: nil, bool: false, revbool: false)
  line = __LINE__ - 1
  file = __FILE__

  [methods].flatten.each do |method|
    prefix = bool || revbool ? "!!#{"!" if revbool}" : ""
    var = key ? %(#{hash}["#{key}"]) : %(#{hash}["#{method}"])
    method = method.to_s + "?" if revbool || bool

    ruby = <<-STR
      def #{method}
        #{prefix}#{var}
      end
    STR

    class_eval ruby, file, line
  end
end

#def_ivar_delegator(var, method, bool: false, revbool: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/forwardable/extended.rb', line 31

def def_ivar_delegator(var, method, bool: false, revbool: false)
  line = __LINE__ - 1
  file = __FILE__

  prefix = revbool || bool ? "!!#{"!" if revbool}" : ""
  method = method.to_s + "?" if revbool || bool
  ruby = <<-STR
    def #{method}
      #{prefix}#{var}
    end
  STR

  class_eval ruby, file, line
end

#def_ivar_delegators(vars, methods, **kwd) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
# File 'lib/forwardable/extended.rb', line 46

def def_ivar_delegators(vars, methods, **kwd)
  raise ArgumentError, "unequal methods and vars" unless vars.size == methods.size
  methods.zip(vars).each do |method, var|
    def_ivar_delegator var, method, **kwd
  end
end