class ActiveRecord::WrappedTransaction::Result

Constants

NOT_CANCELLED
TXN_COMPLETE

Attributes

cancellation_reason[R]

@!attribute [r] cancellation_reason @return [Object, nil]

error[R]

@!attribute [r] error @return [Exception, nil]

Public Class Methods

new(transactor: ActiveRecord::Base, parent_context: nil, **options) { |context| ... } click to toggle source

@param [#transaction, ActiveRecord::ConnectionAdapters::AbstractAdapter] transactor @param [ActiveRecord::WrappedTransaction::Context] parent_context @param [{ Symbol => Object }] options @option options [Boolean] :requires_new @option options [String] :isolation @option options [Boolean] :joinable @yield A block that goes in the transaction. @yieldreturn [Object]

# File lib/activerecord/wrapped_transaction/result.rb, line 26
def initialize(transactor: ActiveRecord::Base, parent_context: nil, **options)
  raise ArgumentError, "Must call with a block to run in the transaction" unless block_given?

  @context = Context.new transactor: transactor, parent: parent_context, **options

  wrap_transaction do
    execute_transaction do
      watch_for_cancellation do
        @result = unwrap yield @context
      end
    end
  end

  freeze
end

Public Instance Methods

cancelled?() click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 42
def cancelled?
  @cancelled
end
result() click to toggle source

@!attribute [r] result @return [Object, nil]

# File lib/activerecord/wrapped_transaction/result.rb, line 48
def result
  @result if success?
end
rolled_back?() click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 52
def rolled_back?
  !success?
end
success?() click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 56
def success?
  @success
end

Private Instance Methods

execute_transaction() { || ... } click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 70
def execute_transaction
  yield
rescue Exception => e
  @error = e

  raise ActiveRecord::Rollback
else
  TXN_COMPLETE
end
unwrap(value) click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 91
def unwrap(value)
  return value unless value.kind_of?(self.class) && value.success?

  value.result
end
watch_for_cancellation() { || ... } click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 80
def watch_for_cancellation
  cancellation_reason = catch(:cancel_transaction) { yield; NOT_CANCELLED }

  return if cancellation_reason.eql? NOT_CANCELLED

  @cancelled = true
  @cancellation_reason = cancellation_reason

  raise ActiveRecord::Rollback, "Cancelled transaction"
end
wrap_transaction() { || ... } click to toggle source
# File lib/activerecord/wrapped_transaction/result.rb, line 62
def wrap_transaction
  caught = @context.wrap_transaction do
    yield
  end
ensure
  @success = caught.eql? TXN_COMPLETE
end