Class: Dry::Operation::Extensions::ROM::Builder Private

Inherits:
Module
  • Object
show all
Defined in:
lib/dry/operation/extensions/rom.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(gateway:) ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.



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

def initialize(gateway:)
  super()
  @gateway = gateway
end

Instance Method Details

#included(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dry/operation/extensions/rom.rb', line 98

def included(klass)
  class_exec(@gateway) do |default_gateway|
    klass.define_method(:transaction) do |gateway: default_gateway, &steps|
      raise Dry::Operation::ExtensionError, <<~MSG unless respond_to?(:rom)
        When using the ROM extension, you need to define a #rom method \
        that returns the ROM container
      MSG

      intercepting_failure do
        result = nil
        rom.gateways[gateway].transaction do |t|
          intercepting_failure(->(failure) {
                                 result = failure
                                 t.rollback!
                               }) do
            result = steps.()
          end
        end
        result
      end
    end
  end
end