Class: MuchResult::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/much-result/transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver, **result_kargs) ⇒ Transaction

Returns a new instance of Transaction.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/much-result/transaction.rb', line 16

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



58
59
60
# File 'lib/much-result/transaction.rb', line 58

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

Class Method Details

.call(receiver, **result_kargs, &block) ⇒ Object



12
13
14
# File 'lib/much-result/transaction.rb', line 12

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

.halt_throw_valueObject



8
9
10
# File 'lib/much-result/transaction.rb', line 8

def self.halt_throw_value
  :muchresult_transaction_halt
end

Instance Method Details

#call(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/much-result/transaction.rb', line 30

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

#haltObject



47
48
49
50
# File 'lib/much-result/transaction.rb', line 47

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

#resultObject



26
27
28
# File 'lib/much-result/transaction.rb', line 26

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

#rollbackObject



42
43
44
45
# File 'lib/much-result/transaction.rb', line 42

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