Module: Rubycom::Executor

Defined in:
lib/rubycom/executor.rb

Class Method Summary collapse

Class Method Details

.execute_command(method, parameters = {}) ⇒ Object

Calls the given method with the given parameters

Parameters:

  • method (Method)

    the Method to call

  • parameters (Hash) (defaults to: {})

    a Hash mapping parameter names to their intended values

Returns:

  • the result of the Method call



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rubycom/executor.rb', line 9

def self.execute_command(method, parameters={})
  raise "#{method} should be a Method but was #{method.class}" if method.class != Method
  raise "#{parameters} should be a Hash but was #{parameters.class}" if parameters.class != Hash
  params = method.parameters.reject{|type,_|type == :rest}.map { |_, sym|
    raise ExecutorError, "parameters should include values for all non * method parameters. Missing value for #{sym.to_s}" unless parameters.has_key?(sym)
    parameters[sym]
  }
  unless method.parameters.select{|type,_|type == :rest}.first.nil? #if there is a * param
    params = params + parameters[method.parameters.select{|type,_|type == :rest}.first[1]] #add in the values which were marked for the * param
  end
  (parameters.nil? || parameters.empty?) ? method.call : method.call(*params)
end