class Substation::Dispatcher

Encapsulates all registered actions and their observers

The only protocol actions must support is +#call(request)+. Actions are intended to be classes that handle one specific application use case.

Constants

GUARD

Public Class Methods

new_registry() click to toggle source

Return a new registry instance suitable for {Dispatcher}

@return [DSL::Registry]

@api private

# File lib/substation/dispatcher.rb, line 22
def self.new_registry
  DSL::Registry.new(GUARD)
end

Public Instance Methods

call(name, input) click to toggle source

Invoke the action identified by name

@example

module App
  class Environment
    def initialize(storage, logger)
      @storage, @logger = storage, logger
    end
  end

  class SomeUseCase
    def self.call(request)
      data = perform_work
      request.success(data)
    end
  end
end

storage    = SomeStorageAbstraction.new
env        = App::Environment.new(storage, Logger.new($stdout))
config     = { :some_use_case => { :action => App::SomeUseCase } }
dispatcher = Substation::Dispatcher.coerce(config, env)

response = dispatcher.call(:some_use_case, :some_input)
response.success? # => true

@param [Symbol] name

a registered action name

@param [Object] input

the input model instance to pass to the action

@return [Response]

the response returned when calling the action

@raise [UnknownActionError]

if no action is registered for +name+

@api public

# File lib/substation/dispatcher.rb, line 66
def call(name, input)
  fetch(name).call(Request.new(name, env, input))
end
include?(name) click to toggle source

Test wether a chain with the given name is registered

@param [Symbol] name

the name of the chain to test for

@return [Boolean]

@api private

# File lib/substation/dispatcher.rb, line 78
def include?(name)
  actions.include?(name)
end

Private Instance Methods

fetch(name) click to toggle source

The action registered with name

@param [Symbol] name

a name for which an action is registered

@return [#call]

the callable registered for +name+

@raise [UnknownActionError]

if no action is registered with +name+

@api private

# File lib/substation/dispatcher.rb, line 96
def fetch(name)
  actions.fetch(name) { raise(UnknownActionError.new(name)) }
end