Class | Transaction::Simple::Group |
In: |
lib/transaction/simple/group.rb
|
Parent: | Object |
A transaction group is an object wrapper that manages a group of objects as if they were a single object for the purpose of transaction management. All transactions for this group of objects should be performed against the transaction group object, not against individual objects in the group.
require 'transaction/simple/group' x = "Hello, you." y = "And you, too." g = Transaction::Simple::Group.new(x, y) g.start_transaction(:first) # -> [ x, y ] g.transaction_open?(:first) # -> true x.transaction_open?(:first) # -> true y.transaction_open?(:first) # -> true x.gsub!(/you/, "world") # -> "Hello, world." y.gsub!(/you/, "me") # -> "And me, too." g.start_transaction(:second) # -> [ x, y ] x.gsub!(/world/, "HAL") # -> "Hello, HAL." y.gsub!(/me/, "Dave") # -> "And Dave, too." g.rewind_transaction(:second) # -> [ x, y ] x # -> "Hello, world." y # -> "And me, too." x.gsub!(/world/, "HAL") # -> "Hello, HAL." y.gsub!(/me/, "Dave") # -> "And Dave, too." g.commit_transaction(:second) # -> [ x, y ] x # -> "Hello, HAL." y # -> "And Dave, too." g.abort_transaction(:first) # -> [ x, y ] x = -> "Hello, you." y = -> "And you, too."
objects | [R] | Returns the objects that are covered by this transaction group. |
Creates a transaction group for the provided objects. If a block is provided, the transaction group object is yielded to the block; when the block is finished, the transaction group object will be cleared with clear.
# File lib/transaction/simple/group.rb, line 59 59: def initialize(*objects) 60: @objects = objects || [] 61: @objects.freeze 62: @objects.each { |obj| obj.extend(Transaction::Simple) } 63: 64: if block_given? 65: begin 66: yield self 67: ensure 68: self.clear 69: end 70: end 71: end
Aborts the transaction. Resets the object state to what it was before the transaction was started and closes the transaction. If name is specified, then the intervening transactions and the named transaction will be aborted. Otherwise, only the current transaction is aborted.
If the current or named transaction has been started by a block (Transaction::Simple.start), then the execution of the block will be halted with break self.
# File lib/transaction/simple/group.rb, line 120 120: def abort_transaction(name = nil) 121: @objects.each { |obj| obj.abort_transaction(name) } 122: end
Clears the object group. Removes references to the objects so that they can be garbage collected.
# File lib/transaction/simple/group.rb, line 78 78: def clear 79: @objects = @objects.dup.clear 80: end
If name is nil (default), the current transaction level is closed out and the changes are committed.
If name is specified and name is in the list of named transactions, then all transactions are closed and committed until the named transaction is reached.
# File lib/transaction/simple/group.rb, line 130 130: def commit_transaction(name = nil) 131: @objects.each { |obj| obj.commit_transaction(name) } 132: end
Rewinds the transaction. If name is specified, then the intervening transactions will be aborted and the named transaction will be rewound. Otherwise, only the current transaction is rewound.
# File lib/transaction/simple/group.rb, line 108 108: def rewind_transaction(name = nil) 109: @objects.each { |obj| obj.rewind_transaction(name) } 110: end
Starts a transaction for the group. Stores the current object state. If a transaction name is specified, the transaction will be named. Transaction names must be unique. Transaction names of nil will be treated as unnamed transactions.
# File lib/transaction/simple/group.rb, line 101 101: def start_transaction(name = nil) 102: @objects.each { |obj| obj.start_transaction(name) } 103: end
Alternative method for calling the transaction methods. An optional name can be specified for named transaction support.
transaction(:start): | start_transaction |
transaction(:rewind): | rewind_transaction |
transaction(:abort): | abort_transaction |
transaction(:commit): | commit_transaction |
transaction(:name): | transaction_name |
transaction: | transaction_open? |
# File lib/transaction/simple/group.rb, line 143 143: def transaction(action = nil, name = nil) 144: @objects.each { |obj| obj.transaction(action, name) } 145: end
Returns the current name of the transaction for the group. Transactions not explicitly named are named nil.
# File lib/transaction/simple/group.rb, line 93 93: def transaction_name 94: @objects[0].transaction_name 95: end
Tests to see if all of the objects in the group have an open transaction. See Transaction::Simple#transaction_open? for more information.
# File lib/transaction/simple/group.rb, line 85 85: def transaction_open?(name = nil) 86: @objects.inject(true) do |val, obj| 87: val = val and obj.transaction_open?(name) 88: end 89: end