Module: Dry::Operation::Extensions::Sequel

Defined in:
lib/dry/operation/extensions/sequel.rb

Overview

Add Sequel transaction support to operations

When this extension is included, you can use a #transaction method to wrap the desired steps in a Sequel transaction. If any of the steps returns a Dry::Monads::Result::Failure, the transaction will be rolled back and, as usual, the rest of the flow will be skipped.

The extension expects the including class to give access to the Sequel database object via a #db method.

class MyOperation < Dry::Operation
  include Dry::Operation::Extensions::Sequel

  attr_reader :db

  def initialize(db:)
    @db = db
  end

  def call(input)
    attrs = step validate(input)
    user = transaction do
      new_user = step persist(attrs)
      step assign_initial_role(new_user)
      new_user
    end
    step notify(user)
    user
  end

  # ...
end

By default, no options are passed to the Sequel transaction. You can change this when including the extension:

include Dry::Operation::Extensions::Sequel[isolation: :serializable]

Or you can change it at runtime:

transaction(isolation: :serializable) do
  # ...
end

WARNING: Be aware that the :savepoint option is not yet supported.

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Class Method Details

.[](options = {}) ⇒ Object

Include the extension providing default options for the transaction.

Parameters:

  • options (Hash) (defaults to: {})

    additional options for the Sequel transaction



73
74
75
# File 'lib/dry/operation/extensions/sequel.rb', line 73

def self.[](options = {})
  Builder.new(**options)
end

.included(klass) ⇒ Object



66
67
68
# File 'lib/dry/operation/extensions/sequel.rb', line 66

def self.included(klass)
  klass.include(self[])
end