Class: ParameterChain

Inherits:
Object show all
Defined in:
lib/parameter_chain.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, callback, params) ⇒ ParameterChain

Returns a new instance of ParameterChain.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/parameter_chain.rb', line 11

def initialize(object, callback, params)
  @object = object
  @callback = callback

  @hash = {}
  params.each do |param|
    __class__.send(:define_method, param) do |arg|
      @hash[param] = arg
      self
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



24
25
26
# File 'lib/parameter_chain.rb', line 24

def method_missing(method, *args)
  @object.send(@callback, @hash).send(method, *args)
end

Class Method Details

.for(klass, callback, params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/parameter_chain.rb', line 28

def self.for(klass, callback, params)
  this = self # Store self for use in block scope.

  params.each do |param|
    klass.send(:define_method, param) do |arg|
      chain = this.new(self, callback, params)
      chain.__send__(param, arg)
    end
  end

  nil
end