Class: MethodChainable::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/method_chainable/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m_chainable, input) ⇒ Proxy

Returns a new instance of Proxy.



14
15
16
17
18
19
# File 'lib/method_chainable/proxy.rb', line 14

def initialize(m_chainable, input)
  @m_chainable = m_chainable
  @output = output
  @input = input
  @m_chain_count = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Remembers the output of the previous call and passes on to the new one



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/method_chainable/proxy.rb', line 33

def method_missing(m, *args, &block)
  m = m.to_s

  # Return the final output if value is called
  if m == 'val'
    return output
  else

    # Since value is not called yet keep calling
    # the actual method on chainable and pass
    # the previous output

    if m_chainable.respond_to? m
      arg = (@m_chain_count == 0) ? input : output
      arity = m_chainable.method(m).arity

      if arity > 0
        @output = m_chainable.send(m, arg)
      else
        @output = m_chainable.send(m)
      end

      @m_chain_count += 1

      return self
    else
      super(m, *args, &block)
    end
  end
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



8
9
10
# File 'lib/method_chainable/proxy.rb', line 8

def input
  @input
end

#m_chain_countObject

Returns the value of attribute m_chain_count.



8
9
10
# File 'lib/method_chainable/proxy.rb', line 8

def m_chain_count
  @m_chain_count
end

#m_chainableObject

Returns the value of attribute m_chainable.



8
9
10
# File 'lib/method_chainable/proxy.rb', line 8

def m_chainable
  @m_chainable
end

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/method_chainable/proxy.rb', line 8

def output
  @output
end