Class: Substation::Dispatcher::Action

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/substation/dispatcher.rb

Overview

Encapsulates access to one registered action

Constant Summary collapse

MissingHandlerError =

Raised when no action class name is configured

Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.coerce(config) ⇒ Action

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce the given name and config to an Substation::Dispatcher::Action instance

Parameters:

  • config (#call, Hash<Symbol, Object>)

    the action configuration object

Returns:

  • (Action)

    the coerced instance

Raises:

  • (MissingHandlerError)

    if no action handler is configured

  • (ArgumentError)

    if action or observer handlers are not coercible



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/substation/dispatcher.rb', line 31

def self.coerce(config)
  if config.respond_to?(:fetch)
    action   = config.fetch(:action) { raise(MissingHandlerError) }
    observer = config[:observer]
  else
    action = config
  end

  action_handler   = Utils.coerce_callable(action)
  observer_handler = Observer.coerce(observer)

  new(action_handler, observer_handler)
end

Instance Method Details

#call(request) ⇒ Substation::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Call the action

Parameters:

Returns:



57
58
59
60
61
# File 'lib/substation/dispatcher.rb', line 57

def call(request)
  response = handler.call(request)
  observer.call(response)
  response
end