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 “‘
Defined Under Namespace
Classes: Builder
Constant Summary collapse
- DEFAULT_GATEWAY =
:default
Class Method Summary collapse
-
.[](gateway: DEFAULT_GATEWAY) ⇒ Object
Include the extension providing a custom gateway.
- .included(klass) ⇒ Object
Instance Method Summary collapse
-
#transaction(gateway: DEFAULT_GATEWAY, &steps) ⇒ Object
Wrap the given steps in a rom transaction.
Class Method Details
.[](gateway: DEFAULT_GATEWAY) ⇒ Object
Include the extension providing a custom gateway
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.
80 81 82 |
# File 'lib/dry/operation/extensions/rom.rb', line 80 def self.included(klass) klass.include(self[]) end |