Module: Dry::Operation::Extensions::ROM

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

Overview

Add rom transaction support to operations

When this extension is included, you can use a ‘#transaction` method to wrap the desired steps in a rom 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 rom container via a ‘#rom` method.

“‘ruby require “dry/operation/extensions/rom”

class MyOperation < Dry::Operation

include Dry::Operation::Extensions::ROM

attr_reader :rom

def initialize(rom:)
  @rom = rom
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, the ‘:default` gateway will be used. You can change this when including the extension:

“‘ruby include Dry::Operation::Extensions::ROM[gateway: :my_gateway] “`

Or you can change it at runtime:

“‘ruby user = transaction(gateway: :my_gateway) do

# ...

end “‘

See Also:

Defined Under Namespace

Classes: Builder

Constant Summary collapse

DEFAULT_GATEWAY =
:default

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](gateway: DEFAULT_GATEWAY) ⇒ Object

Include the extension providing a custom gateway

Parameters:

  • gateway (Symbol) (defaults to: DEFAULT_GATEWAY)

    the rom gateway to use



87
88
89
# File 'lib/dry/operation/extensions/rom.rb', line 87

def self.[](gateway: DEFAULT_GATEWAY)
  Builder.new(gateway: gateway)
end

.included(klass) ⇒ Object



80
81
82
# File 'lib/dry/operation/extensions/rom.rb', line 80

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

Instance Method Details

#transaction(gateway: DEFAULT_GATEWAY, &steps) ⇒ Object

Wrap the given steps in a rom transaction.

If any of the steps returns a ‘Dry::Monads::Result::Failure`, the transaction will be rolled back and `:halt` will be thrown with the failure as its value.

Yield Returns:

  • (Object)

    the result of the block

Raises:

See Also:



80
81
82
# File 'lib/dry/operation/extensions/rom.rb', line 80

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