Class: Substation::Chain
- Inherits:
-
Object
- Object
- Substation::Chain
- Includes:
- Adamantium::Flat
- Defined in:
- lib/substation/chain.rb,
lib/substation/chain/dsl.rb,
lib/substation/chain/definition.rb,
lib/substation/chain/dsl/config.rb,
lib/substation/chain/dsl/module_builder.rb
Overview
Implements a chain of responsibility for an action
An instance of this class will typically contain (in that order) a few processors that process the incoming Request object, one processor that calls an action (Processor::Pivot), and some processors that process the outgoing Response object.
Defined Under Namespace
Classes: DSL, Definition
Constant Summary collapse
- EMPTY =
Empty chain
new(Definition::EMPTY, EMPTY_ARRAY)
Class Method Summary collapse
-
.exception_response(state, data, exception) ⇒ Response::Exception
private
Return an exception response.
Instance Method Summary collapse
-
#call(request) ⇒ Response::Success, ...
Call the chain.
Class Method Details
.exception_response(state, data, exception) ⇒ Response::Exception
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 an exception response
90 91 92 93 |
# File 'lib/substation/chain.rb', line 90 def self.exception_response(state, data, exception) output = Response::Exception::Output.new(data, exception) Response::Exception.new(state.to_request(data), output) end |
Instance Method Details
#call(request) ⇒ Response::Success, ...
Call the chain
Invokes all processors and returns either the first Response::Failure that it encounters, or if all goes well, the Response::Success returned from the last processor.
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/substation/chain.rb', line 137 def call(request) reduce(request) { |result, processor| begin response = processor.call(result) return response unless processor.success?(response) processor.result(response) rescue => exception return on_exception(request, result.data, exception) end } end |