Class: Transaction::Simple::Group
- Inherits:
-
Object
- Object
- Transaction::Simple::Group
- Defined in:
- lib/transaction/simple/group.rb
Overview
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.
Transaction Group Usage
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."
Direct Known Subclasses
Instance Attribute Summary collapse
-
#objects ⇒ Object
readonly
Returns the objects that are covered by this transaction group.
Instance Method Summary collapse
-
#abort_transaction(name = nil) ⇒ Object
Aborts the transaction.
-
#clear ⇒ Object
Clears the object group.
-
#commit_transaction(name = nil) ⇒ Object
If
name
isnil
(default), the current transaction level is closed out and the changes are committed. -
#initialize(*objects) ⇒ Group
constructor
Creates a transaction group for the provided objects.
-
#rewind_transaction(name = nil) ⇒ Object
Rewinds the transaction.
-
#start_transaction(name = nil) ⇒ Object
Starts a transaction for the group.
-
#transaction(action = nil, name = nil) ⇒ Object
Alternative method for calling the transaction methods.
-
#transaction_name ⇒ Object
Returns the current name of the transaction for the group.
-
#transaction_open?(name = nil) ⇒ Boolean
Tests to see if all of the objects in the group have an open transaction.
Constructor Details
#initialize(*objects) ⇒ 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/transaction/simple/group.rb', line 47 def initialize(*objects) @objects = objects || [] @objects.freeze @objects.each { |obj| obj.extend(Transaction::Simple) } if block_given? begin yield self ensure self.clear end end end |
Instance Attribute Details
#objects ⇒ Object (readonly)
Returns the objects that are covered by this transaction group.
62 63 64 |
# File 'lib/transaction/simple/group.rb', line 62 def objects @objects end |
Instance Method Details
#abort_transaction(name = nil) ⇒ Object
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
.
108 109 110 |
# File 'lib/transaction/simple/group.rb', line 108 def abort_transaction(name = nil) @objects.each { |obj| obj.abort_transaction(name) } end |
#clear ⇒ Object
Clears the object group. Removes references to the objects so that they can be garbage collected.
66 67 68 |
# File 'lib/transaction/simple/group.rb', line 66 def clear @objects = @objects.dup.clear end |
#commit_transaction(name = nil) ⇒ Object
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.
118 119 120 |
# File 'lib/transaction/simple/group.rb', line 118 def commit_transaction(name = nil) @objects.each { |obj| obj.commit_transaction(name) } end |
#rewind_transaction(name = nil) ⇒ Object
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.
96 97 98 |
# File 'lib/transaction/simple/group.rb', line 96 def rewind_transaction(name = nil) @objects.each { |obj| obj.rewind_transaction(name) } end |
#start_transaction(name = nil) ⇒ Object
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.
89 90 91 |
# File 'lib/transaction/simple/group.rb', line 89 def start_transaction(name = nil) @objects.each { |obj| obj.start_transaction(name) } end |
#transaction(action = nil, name = nil) ⇒ Object
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?
131 132 133 |
# File 'lib/transaction/simple/group.rb', line 131 def transaction(action = nil, name = nil) @objects.each { |obj| obj.transaction(action, name) } end |
#transaction_name ⇒ Object
Returns the current name of the transaction for the group. Transactions not explicitly named are named nil
.
81 82 83 |
# File 'lib/transaction/simple/group.rb', line 81 def transaction_name @objects[0].transaction_name end |
#transaction_open?(name = nil) ⇒ Boolean
Tests to see if all of the objects in the group have an open transaction. See Transaction::Simple#transaction_open? for more information.
73 74 75 76 77 |
# File 'lib/transaction/simple/group.rb', line 73 def transaction_open?(name = nil) @objects.inject(true) do |val, obj| val = val and obj.transaction_open?(name) end end |