Module: Dry

Defined in:
lib/dry/transaction.rb,
lib/dry/transaction/dsl.rb,
lib/dry/transaction/step.rb,
lib/dry/transaction/stack.rb,
lib/dry/transaction/errors.rb,
lib/dry/transaction/builder.rb,
lib/dry/transaction/version.rb,
lib/dry/transaction/callable.rb,
lib/dry/transaction/operation.rb,
lib/dry/transaction/step_adapter.rb,
lib/dry/transaction/step_failure.rb,
lib/dry/transaction/step_adapters.rb,
lib/dry/transaction/result_matcher.rb,
lib/dry/transaction/instance_methods.rb,
lib/dry/transaction/step_adapters/map.rb,
lib/dry/transaction/step_adapters/raw.rb,
lib/dry/transaction/step_adapters/tee.rb,
lib/dry/transaction/step_adapters/try.rb,
lib/dry/transaction/operation_resolver.rb,
lib/dry/transaction/step_adapters/check.rb,
lib/dry/transaction/step_adapters/around.rb

Defined Under Namespace

Modules: Transaction

Class Method Summary collapse

Class Method Details

.Transaction(container: nil, step_adapters: Transaction::StepAdapters) ⇒ Module

Build a module to make your class a business transaction.

A business transaction is a series of callable operation objects that receive input and produce an output.

The operations can be instance methods, or objects addressable via ‘#[]` in a container object that you pass when mixing in this module. The operations must respond to `#call(input, *args)`.

Each operation will be called in the order it was specified in your transaction, with its output passed as the input to the next operation. Operations will only be called if the previous step was a success.

A step is successful when it returns a [dry-monads](dry-monads) ‘Success` object wrapping its output value. A step is a failure when it returns a `Failure` object. If your operations already return a `Success` or `Failure`, they can be added to your operation as plain `step` steps.

Add operation to your transaction with the ‘step` method.

If your operations don’t already return ‘Success` or `Failure`, then they can be added to the transaction with the following steps:

  • ‘check` — wrap original input in `Success` or `Failure` according to operation return value, and pass it as output.

  • ‘map` — wrap the output of the operation in a `Success`

  • ‘try` — wrap the output of the operation in a `Success`, unless a certain exception is raised, which will be caught and returned as a `Failure`.

  • ‘tee` — ignore the output of the operation and pass through its original input as a `Success`

[dry-monads]: rubygems.org/gems/dry-monads

custom container of step adapters

Examples:

class MyTransaction
  include Dry::Transaction(container: MyContainer)

  step :first_step, with: "my_container.operations.first"
  step :second_step

  def second_step(input)
    result = do_something_with(input)
    Success(result)
  end
end

my_transaction = MyTransaction.new
my_transaction.call(some_input)

Parameters:

  • container (#[]) (defaults to: nil)

    the operations container

  • step_adapters (#[]) (defaults to: Transaction::StepAdapters)

    (Dry::Transaction::StepAdapters) a

Returns:

  • (Module)

    the transaction module



71
72
73
# File 'lib/dry/transaction.rb', line 71

def self.Transaction(container: nil, step_adapters: Transaction::StepAdapters)
  Transaction::Builder.new(container: container, step_adapters: step_adapters)
end