Class: Substation::Dispatcher

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

Overview

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.

Constant Summary collapse

GUARD =
DSL::Guard.new(EMPTY_ARRAY)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_registryDSL::Registry

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.

Return a new registry instance suitable for Substation::Dispatcher



22
23
24
# File 'lib/substation/dispatcher.rb', line 22

def self.new_registry
  DSL::Registry.new(GUARD)
end

Instance Method Details

#call(name, input) ⇒ Response

Invoke the action identified by name

Examples:


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

Raises:



66
67
68
# File 'lib/substation/dispatcher.rb', line 66

def call(name, input)
  fetch(name).call(Request.new(name, env, input))
end

#include?(name) ⇒ Boolean

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.

Test wether a chain with the given name is registered



78
79
80
# File 'lib/substation/dispatcher.rb', line 78

def include?(name)
  actions.include?(name)
end