Class: Atacama::Transaction
- Includes:
- Values::Methods
- Defined in:
- lib/atacama/transaction.rb
Defined Under Namespace
Classes: Result
Constant Summary
Constants inherited from Contract
Contract::ContextInterface, Contract::NameInterface, Contract::RESERVED_KEYS, Contract::Types
Class Attribute Summary collapse
-
.return_option ⇒ Object
readonly
Returns the value of attribute return_option.
Attributes inherited from Contract
Class Method Summary collapse
- .add_step(params) ⇒ Object
- .inherited(subclass) ⇒ Object
-
.returns_option(key, type = nil) ⇒ Object
Return the value of a given Option in the pipeline.
-
.step(name, with: nil, yielding: nil) { ... } ⇒ Object
Add a step to the processing queue.
- .steps ⇒ Object
Instance Method Summary collapse
-
#call ⇒ Atacama::Transaction::Result
Trigger execution of the Transaction pipeline.
-
#initialize(context: {}, steps: {}) ⇒ Transaction
constructor
A new instance of Transaction.
Methods included from Values::Methods
Methods inherited from Contract
call, inject, injected, injected=, #inspect, option, options, return_type, returns, validate_return
Constructor Details
#initialize(context: {}, steps: {}) ⇒ Transaction
Returns a new instance of Transaction.
81 82 83 84 85 |
# File 'lib/atacama/transaction.rb', line 81 def initialize(context: {}, steps: {}) super(context: context) @overrides = steps @return_value = nil end |
Class Attribute Details
.return_option ⇒ Object (readonly)
Returns the value of attribute return_option.
18 19 20 |
# File 'lib/atacama/transaction.rb', line 18 def return_option @return_option end |
Class Method Details
.add_step(params) ⇒ Object
71 72 73 |
# File 'lib/atacama/transaction.rb', line 71 def add_step(params) steps.push(Definition.call(params)) end |
.inherited(subclass) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/atacama/transaction.rb', line 20 def inherited(subclass) super(subclass) subclass.returns_option return_option, return_type steps.each do |step| subclass.step(step.name, with: step.with, yielding: step.yielding) end end |
.returns_option(key, type = nil) ⇒ Object
Return the value of a given Option in the pipeline.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/atacama/transaction.rb', line 32 def returns_option(key, type = nil) @return_option = key returns( Types.Instance(Result).constructor do || Atacama.check(type, .value) do |e| raise ResultTypeMismatchError, Atacama.format_exception(self, e, "Option(#{key.inspect}) returned from the Transaction was the incorrect type.", ) end end ) end |
.step(name, with: nil, yielding: nil) { ... } ⇒ Object
Add a step to the processing queue.
62 63 64 65 66 67 68 |
# File 'lib/atacama/transaction.rb', line 62 def step(name, with: nil, yielding: nil, &block) add_step({ name: name, with: with, yielding: yielding || block_given? ? Class.new(self, &block) : nil }) end |
.steps ⇒ Object
76 77 78 |
# File 'lib/atacama/transaction.rb', line 76 def steps @steps ||= [] end |
Instance Method Details
#call ⇒ Atacama::Transaction::Result
Trigger execution of the Transaction pipeline.
90 91 92 93 |
# File 'lib/atacama/transaction.rb', line 90 def call execute(self.class.steps) Result.call(value: return_value, transaction: context) end |