Class: ResourceKit::MethodFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_kit/method_factory.rb

Class Method Summary collapse

Class Method Details

.construct(object, resource_collection, invoker = ActionInvoker) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/resource_kit/method_factory.rb', line 3

def self.construct(object, resource_collection, invoker = ActionInvoker)
  resource_collection.each do |action|
    if object.method_defined?(action.name)
      raise ArgumentError, "Action '#{action.name}' is already defined on `#{object}`"
    end
    method_block = method_for_action(action, invoker)

    object.send(:define_method, action.name, &method_block)
  end
end

.method_for_action(action, invoker) ⇒ Object



14
15
16
17
18
# File 'lib/resource_kit/method_factory.rb', line 14

def self.method_for_action(action, invoker)
  Proc.new do |*args|
    invoker.call(action, connection, *args)
  end
end