Class: Transflow::Transaction
- Inherits:
-
Object
- Object
- Transflow::Transaction
- Defined in:
- lib/transflow/transaction.rb
Overview
Transaction encapsulates calling individual steps registered within a transflow constructor.
It’s responsible for calling steps in the right order and optionally currying arguments for specific steps.
Furthermore you can subscribe event listeners to individual steps within a transaction.
Defined Under Namespace
Classes: Step
Instance Attribute Summary collapse
- #step_names ⇒ Object readonly private
- #steps ⇒ Object readonly private
Instance Method Summary collapse
-
#call(input, options = {}) ⇒ Object
(also: #[])
Call the transaction.
-
#initialize(steps) ⇒ Transaction
constructor
private
A new instance of Transaction.
-
#subscribe(listeners) ⇒ self
Subscribe event listeners to specific steps.
-
#to_s ⇒ String
Coerce a transaction into string representation.
Constructor Details
#initialize(steps) ⇒ Transaction
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.
Returns a new instance of Transaction.
43 44 45 46 |
# File 'lib/transflow/transaction.rb', line 43 def initialize(steps) @steps = steps @step_names = steps.keys.reverse end |
Instance Attribute Details
#step_names ⇒ Object (readonly)
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.
40 41 42 |
# File 'lib/transflow/transaction.rb', line 40 def step_names @step_names end |
#steps ⇒ Object (readonly)
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.
35 36 37 |
# File 'lib/transflow/transaction.rb', line 35 def steps @steps end |
Instance Method Details
#call(input, options = {}) ⇒ Object Also known as: []
Call the transaction
Once transaction is called it will call the first step and its result will be passed to the second step and so on.
109 110 111 112 113 114 |
# File 'lib/transflow/transaction.rb', line 109 def call(input, = {}) handler = handler_steps().map(&method(:step)).reduce(:>>) handler.call(input) rescue StepError => err raise TransactionFailedError.new(self, err) end |
#subscribe(listeners) ⇒ self
Subscribe event listeners to specific steps
74 75 76 77 78 79 80 81 |
# File 'lib/transflow/transaction.rb', line 74 def subscribe(listeners) if listeners.is_a?(Hash) listeners.each { |step, listener| steps[step].subscribe(listener) } else steps.each { |(_, step)| step.subscribe(listeners) } end self end |
#to_s ⇒ String
Coerce a transaction into string representation
122 123 124 |
# File 'lib/transflow/transaction.rb', line 122 def to_s "Transaction(#{step_names.join(' => ')})" end |