Class: Object

Inherits:
BasicObject
Defined in:
lib/testable/extensions/data_setter.rb

Instance Method Summary collapse

Instance Method Details

#chain(method_chain, data = nil) ⇒ Object

This method is necessary to dynamically chain method calls. The reason this is necessary the data setter initially has no idea of the actual object it’s going to be dealing with, particularly because part of its job is to find that object and map a data string to it. Not only this, but that element will have been called on a specific instance of a interface class. With the example provide in the comments below for the ‘using` method, the following would be the case:

method_chain: warp_factor.set o (object): <WarpTravel:0x007f8b23224218> m (method): warp_factor data: 1

Thus what you end up with is:

<WarpTravel:0x007f8b23224218>.warp_factor.set 1


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/testable/extensions/data_setter.rb', line 18

def chain(method_chain, data = nil)
  return self if method_chain.empty?

  method_chain.split('.').inject(self) do |o, m|
    if data.nil?
      o.public_send(m.to_sym)
    else
      o.public_send(m.to_sym, data)
    end
  end
end