Module: Rainman::Driver

Includes:
ActionMethods
Defined in:
lib/rainman/driver.rb

Overview

The Rainman::Driver module contains methods for defining Drivers and proxying their associated actions to the appropriate handlers.

Defined Under Namespace

Modules: ActionMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActionMethods

#create_method, #define_action

Class Method Details

.allObject

Public: Get a list of all Drivers (eg: Modules that are extended with Rainman::Driver).

Returns an Array.



18
19
20
# File 'lib/rainman/driver.rb', line 18

def self.all
  @all ||= []
end

.extended(base) ⇒ Object

Public: Extended hook; this is run when a module extends itself with the Rainman::Driver module.



9
10
11
12
# File 'lib/rainman/driver.rb', line 9

def self.extended(base)
  all << base
  base.extend(base)
end

Instance Method Details

#default_handlerObject

Public: Get the default handler used for this Driver.

Returns the Symbol name of this Driver’s default handler.



84
85
86
# File 'lib/rainman/driver.rb', line 84

def default_handler
  @default_handler
end

#handlersObject

Public: Registered handlers.

Keys are the handler name (eg: :my_handler); values are the handler class (eg: MyHandler).

Raises NoHandler if an attempt to access a key of nil is made, (eg: handlers).

Raises InvalidHandler if an attempt to access an invalid key is made.

Returns a Hash.



33
34
35
36
37
38
39
40
41
# File 'lib/rainman/driver.rb', line 33

def handlers
  @handlers ||= Hash.new do |hash, key|
    if key.nil?
      raise NoHandler
    else
      raise InvalidHandler, key
    end
  end
end

#set_current_handler(name) ⇒ Object

Public: Sets the current handler. Name should be an underscored symbol representing a class name in the current context.

name - The Symbol name of the handler to use. Can be set to nil to

clear the current handler.

Example

set_current_handler :my_handler #=> sets handler to MyHandler
set_current_handler nil         #=> clears handler

Returns the Symbol name of the current handler or nothing.



100
101
102
# File 'lib/rainman/driver.rb', line 100

def set_current_handler(name)
  @current_handler = name
end

#set_default_handler(name) ⇒ Object

Public: Sets the default handler used for this Driver.

name - The Symbol name to set as the default handler. Should be a key

from handlers.

Returns the Symbol name.



77
78
79
# File 'lib/rainman/driver.rb', line 77

def set_default_handler(name)
  @default_handler = name
end

#with_handler(name, &block) ⇒ Object

Public: Temporarily change a Driver’s current handler. The handler is changed for the duration of the block supplied. This is useful to perform actions using multiple handlers without changing defaults.

name - The Symbol name of the handler to use.

Example

with_handler(:enom) do |handler|
  handler.transfer
end

Yields a Runner instance if a block is given.

Returns a Runner instance or the result of a block.

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rainman/driver.rb', line 58

def with_handler(name, &block)
  raise MissingBlock, :with_handler unless block_given?

  old_handler = current_handler

  begin
    set_current_handler name
    yield current_handler_instance.runner
  ensure
    set_current_handler old_handler
  end
end