Class: Substation::Chain
- Inherits:
-
Object
- Object
- Substation::Chain
- Includes:
- Adamantium::Flat, Enumerable, Outgoing
- Defined in:
- lib/substation/chain.rb,
lib/substation/chain/dsl.rb
Overview
Implements a chain of responsibility for an action
An instance of this class will typically contain (in that order) a few handlers that process the incoming Request object, one handler that calls an action (Pivot), and some handlers that process the outgoing Response object.
Both Incoming and Outgoing handlers must respond to ‘#call(response)` and `#result(response)`.
Defined Under Namespace
Modules: Incoming, Outgoing Classes: DSL
Constant Summary collapse
Class Method Summary collapse
-
.build(dsl, other, &block) ⇒ Chain
private
Build a new chain instance.
Instance Method Summary collapse
-
#call(request) ⇒ Response::Success, Response::Failure
Call the chain.
-
#each(&block) {|handler| ... } ⇒ self
private
Iterate over all processors.
Methods included from Outgoing
Class Method Details
.build(dsl, other, &block) ⇒ Chain
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.
Build a new chain instance
19 20 21 |
# File 'lib/substation/chain/dsl.rb', line 19 def self.build(dsl, other, &block) new(dsl.processors(other, &block)) end |
Instance Method Details
#call(request) ⇒ Response::Success, Response::Failure
Call the chain
Invokes all handlers and returns either the first Response::Failure that it encounters, or if all goes well, the Response::Success returned from the last handler.
174 175 176 177 178 179 180 |
# File 'lib/substation/chain.rb', line 174 def call(request) handlers.inject(request) { |result, handler| response = handler.call(result) return response unless response.success? handler.result(response) } end |
#each(&block) {|handler| ... } ⇒ self
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.
Iterate over all processors
195 196 197 198 199 |
# File 'lib/substation/chain.rb', line 195 def each(&block) return to_enum unless block handlers.each(&block) self end |