Module: Rufus::Tokyo::Transactions

Included in:
Edo::CabinetCore, Edo::TableCore, Cabinet, Table
Defined in:
lib/rufus/tokyo/transactions.rb

Overview

A mixin for structures to respond to tranbegin, trancommit and tranabort

Defined Under Namespace

Classes: Abort

Instance Method Summary collapse

Instance Method Details

#abortObject

Aborts the enclosing transaction

See #transaction

Raises:



70
71
72
# File 'lib/rufus/tokyo/transactions.rb', line 70

def abort
  raise Abort, "abort transaction !"
end

#transactionObject

Transaction in a block.

table.transaction do
  table['pk0'] => { 'name' => 'Fred', 'age' => '40' }
  table['pk1'] => { 'name' => 'Brooke', 'age' => '76' }
  table.abort if weather.bad?
end

(This is a table example, a classical cabinet won't accept a hash as a value for its entries).

If an error or an abort is trigger withing the transaction, it's rolled back. If the block executes successfully, it gets commited.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rufus/tokyo/transactions.rb', line 49

def transaction

  return unless block_given?

  begin
    tranbegin
    yield
    trancommit
  rescue Rufus::Tokyo::Transactions::Abort
    tranabort
  rescue Exception => e
    tranabort
    raise e
  end
end