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
Modules: Registry
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.
48 49 50 51 |
# File 'lib/transflow/transaction.rb', line 48 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.
45 46 47 |
# File 'lib/transflow/transaction.rb', line 45 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.
40 41 42 |
# File 'lib/transflow/transaction.rb', line 40 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.
110 111 112 113 114 115 |
# File 'lib/transflow/transaction.rb', line 110 def call(input, = {}) handler = handler_steps().map(&method(:fn)).reduce(:>>) handler.call(input) rescue Transproc::MalformedInputError => err raise TransactionFailedError.new(self, err.original_error) end |
#subscribe(listeners) ⇒ self
Subscribe event listeners to specific steps
79 80 81 82 |
# File 'lib/transflow/transaction.rb', line 79 def subscribe(listeners) listeners.each { |step, listener| steps[step].subscribe(listener) } self end |
#to_s ⇒ String
Coerce a transaction into string representation
123 124 125 |
# File 'lib/transflow/transaction.rb', line 123 def to_s "Transaction(#{step_names.join(' => ')})" end |