Class: Atacama::Transaction

Inherits:
Contract show all
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

Attributes inherited from Contract

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Values::Methods

#Option, #Return

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_optionObject (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.

Parameters:

  • key (Symbol)

    the option to read

  • type (Dry::Type?) (defaults to: nil)

    the type object to optionally check



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 |options|
      Atacama.check(type, options.value) do |e|
        raise ResultTypeMismatchError, Atacama.format_exception(self, e,
          "Option(#{key.inspect}) returned from the Transaction was the incorrect type.",
        )
      end

      options
    end
  )
end

.step(name, with: nil, yielding: nil) { ... } ⇒ Object

Add a step to the processing queue.

Examples:

step :extract, with: UserParamsExtractor

a yielding step

step :wrap, with: Wrapper do
  step :extract, with: UserParamsExtractor
end

Parameters:

  • name (Symbol)

    a unique name for a step

  • with (Contract, Proc, nil) (defaults to: nil)

    the callable to execute

Yields:

  • The captured block allows defining of child steps. The wrapper must implement yield.



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

.stepsObject



76
77
78
# File 'lib/atacama/transaction.rb', line 76

def steps
  @steps ||= []
end

Instance Method Details

#callAtacama::Transaction::Result

Trigger execution of the Transaction pipeline.

Returns:



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