Sha256: 6227849835b36eabe17b505d88f04cace8fd960cff7a1b3ad327cb25df2ece84

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require "much-result"

class MuchResult; end
class MuchResult::Transaction
  def self.halt_throw_value
    :muchresult_transaction_halt
  end

  def self.call(receiver, **result_kargs, &block)
    new(receiver, **result_kargs).call(&block)
  end

  def initialize(receiver, **result_kargs)
    raise(ArgumentError, "`receiver` can't be nil.") if receiver.nil?

    @receiver = receiver
    @result_kargs = result_kargs

    result.much_result_transaction_rolled_back = false
    result.much_result_transaction_halted = false
  end

  def result
    @result ||= MuchResult.success(**@result_kargs)
  end

  def call(&block)
    begin
      @receiver.transaction do
        catch(self.class.halt_throw_value) { block.call(self) }
      end
    rescue MuchResult::Rollback
      # do nothing
    end

    result
  end

  def rollback
    result.much_result_transaction_rolled_back = true
    raise MuchResult::Rollback
  end

  def halt
    result.much_result_transaction_halted = true
    throw(self.class.halt_throw_value)
  end

  private

  def respond_to_missing?(*args)
    result.send(:respond_to_missing?, *args)
  end

  def method_missing(method, *args, &block)
    result.public_send(method, *args, &block)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
much-result-0.1.2 lib/much-result/transaction.rb
much-result-0.1.1 lib/much-result/transaction.rb