|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "dry/operation/errors" |
| 4 | + |
| 5 | +begin |
| 6 | + require "rom-sql" |
| 7 | +rescue LoadError |
| 8 | + raise Dry::Operation::MissingDependencyError.new(gem: "rom-sql", extension: "ROM") |
| 9 | +end |
| 10 | + |
| 11 | +module Dry |
| 12 | + class Operation |
| 13 | + module Extensions |
| 14 | + # Add rom transaction support to operations |
| 15 | + # |
| 16 | + # When this extension is included, you can use a `#transaction` method |
| 17 | + # to wrap the desired steps in a rom transaction. If any of the steps |
| 18 | + # returns a `Dry::Monads::Result::Failure`, the transaction will be rolled |
| 19 | + # back and, as usual, the rest of the flow will be skipped. |
| 20 | + # |
| 21 | + # The extension expects the including class to give access to the rom |
| 22 | + # container via a `#rom` method. |
| 23 | + # |
| 24 | + # ```ruby |
| 25 | + # class MyOperation < Dry::Operation |
| 26 | + # include Dry::Operation::Extensions::ROM |
| 27 | + # |
| 28 | + # attr_reader :rom |
| 29 | + # |
| 30 | + # def initialize(rom:) |
| 31 | + # @rom = rom |
| 32 | + # end |
| 33 | + # |
| 34 | + # def call(input) |
| 35 | + # attrs = step validate(input) |
| 36 | + # user = transaction do |
| 37 | + # new_user = step persist(attrs) |
| 38 | + # step assign_initial_role(new_user) |
| 39 | + # new_user |
| 40 | + # end |
| 41 | + # step notify(user) |
| 42 | + # user |
| 43 | + # end |
| 44 | + # |
| 45 | + # # ... |
| 46 | + # end |
| 47 | + # ``` |
| 48 | + # |
| 49 | + # By default, the `:default` gateway will be used. You can change this |
| 50 | + # when including the extension: |
| 51 | + # |
| 52 | + # ```ruby |
| 53 | + # include Dry::Operation::Extensions::ROM[gateway: :my_gateway] |
| 54 | + # ``` |
| 55 | + # |
| 56 | + # Or you can change it at runtime: |
| 57 | + # |
| 58 | + # ```ruby |
| 59 | + # user = transaction(gateway: :my_gateway) do |
| 60 | + # # ... |
| 61 | + # end |
| 62 | + # ``` |
| 63 | + # |
| 64 | + # @see https://rom-rb.org |
| 65 | + module ROM |
| 66 | + DEFAULT_GATEWAY = :default |
| 67 | + |
| 68 | + # @!method transaction(gateway: DEFAULT_GATEWAY, &steps) |
| 69 | + # Wrap the given steps in a rom transaction. |
| 70 | + # |
| 71 | + # If any of the steps returns a `Dry::Monads::Result::Failure`, the |
| 72 | + # transaction will be rolled back and `:halt` will be thrown with the |
| 73 | + # failure as its value. |
| 74 | + # |
| 75 | + # @yieldreturn [Object] the result of the block |
| 76 | + # @see Dry::Operation#steps |
| 77 | + |
| 78 | + def self.included(klass) |
| 79 | + klass.include(self[]) |
| 80 | + end |
| 81 | + |
| 82 | + # Include the extension providing a custom gateway |
| 83 | + # |
| 84 | + # @param gateway [Symbol] the rom gateway to use |
| 85 | + def self.[](gateway: DEFAULT_GATEWAY) |
| 86 | + Builder.new(gateway: gateway) |
| 87 | + end |
| 88 | + |
| 89 | + # @api private |
| 90 | + class Builder < Module |
| 91 | + def initialize(gateway:) |
| 92 | + super() |
| 93 | + @gateway = gateway |
| 94 | + end |
| 95 | + |
| 96 | + def included(klass) |
| 97 | + class_exec(@gateway) do |default_gateway| |
| 98 | + klass.define_method(:transaction) do |gateway: default_gateway, &steps| |
| 99 | + raise Dry::Operation::InterfaceNotImplementedError, <<~MSG unless respond_to?(:rom) |
| 100 | + When using the ROM extension, you need to define a #rom method \ |
| 101 | + that returns the ROM container |
| 102 | + MSG |
| 103 | + |
| 104 | + rom.gateways[gateway].transaction do |t| |
| 105 | + intercepting_failure(-> { raise t.rollback! }) do |
| 106 | + steps.() |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments