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 and no additional options will be passed to the Rom transaction.

You can change the default gateway and transaction options when including the extension:

“‘ruby include Dry::Operation::Extensions::ROM[

gateway: :my_gateway,
isolation: :serializable

] “‘

You can also override the gateway and/or transaction options at runtime:

“‘ruby user = transaction(

gateway: :my_gateway,
isolation: :serializable

) 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, **options) ⇒ Object

Include the extension providing a custom gateway



95
96
97
# File 'lib/dry/operation/extensions/rom.rb', line 95

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

.included(klass) ⇒ Object



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

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:



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

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