Class: Rainman::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/rainman/runner.rb

Overview

The Runner class delegates actions to handlers. It runs validations before executing the action.

Examples

Runner.new(current_handler_instance).tap do |r|
  r.transfer
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Runner

Public: Initialize a runner.

handler - A handler Class instance.

Examples

Runner.new(current_handler_instance)


21
22
23
# File 'lib/rainman/runner.rb', line 21

def initialize(handler)
  @handler = handler
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Internal: Method missing hook used to proxy methods to a handler.

method - The missing method name. args - Arguments to be supplied to the method (optional). block - Block to be supplied to the method (optional).

Raises NameError if handler does not respond to method.

Returns the value of execute.



71
72
73
74
75
76
77
78
79
# File 'lib/rainman/runner.rb', line 71

def method_missing(method, *args, &block)
  if handler.respond_to?(method)
    execute(handler, method, *args, &block)
  elsif parent_klass.respond_to?(method)
    execute(parent_klass, method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#handlerObject (readonly)

Public: Gets the handler Class.



12
13
14
# File 'lib/rainman/runner.rb', line 12

def handler
  @handler
end

Instance Method Details

#execute(context, method, *args, &block) ⇒ Object

Public: Delegates the given method to the handler.

context - Set the context for the method (class/instance) method - The method to send to the handler. args - Arguments to be supplied to the method (optional). block - Block to be supplied to the method (optional).

Examples

execute(handler, :register)
execute(handler.parent_class, :register, { params: [] })
execute(handler, :register, :one, :argument) do
  # some code
end

Raises MissingParameter if validation fails due to missing parameters.

Returns the result of the handler action.



57
58
59
60
# File 'lib/rainman/runner.rb', line 57

def execute(context, method, *args, &block)
  # verify params here
  context.send(method, *args, &block)
end

#nameObject

Public: Get the Symbol name of the handler.

Returns a Symbol.



28
29
30
# File 'lib/rainman/runner.rb', line 28

def name
  handler.class.handler_name
end

#parent_klassObject

Public: Get the handler’s parent_klass

Returns Rainman::Driver.self



35
36
37
# File 'lib/rainman/runner.rb', line 35

def parent_klass
  handler.class.parent_klass
end