Class: ODB::Transaction
Instance Attribute Summary collapse
-
#objects ⇒ Object
Returns the value of attribute objects.
Class Method Summary collapse
Instance Method Summary collapse
- #commit ⇒ Object
-
#initialize(db = ODB.current, &block) ⇒ Transaction
constructor
A new instance of Transaction.
- #persistent_objects ⇒ Object
- #transaction(&block) ⇒ Object
Constructor Details
#initialize(db = ODB.current, &block) ⇒ Transaction
Returns a new instance of Transaction.
83 84 85 86 87 |
# File 'lib/odb.rb', line 83 def initialize(db = ODB.current, &block) @objects = Set.new @db = db transaction(&block) if block_given? end |
Instance Attribute Details
#objects ⇒ Object
Returns the value of attribute objects.
73 74 75 |
# File 'lib/odb.rb', line 73 def objects @objects end |
Class Method Details
.current ⇒ Object
79 80 81 |
# File 'lib/odb.rb', line 79 def self.current transactions.last end |
.transactions ⇒ Object
75 76 77 |
# File 'lib/odb.rb', line 75 def self.transactions Thread.current['__odb_transactions'] ||= [] end |
Instance Method Details
#commit ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/odb.rb', line 98 def commit self.objects = objects.to_a while objects.size > 0 object = objects.pop @db.store[object.object_id] = object end @db.store.after_commit end |
#persistent_objects ⇒ Object
107 108 109 |
# File 'lib/odb.rb', line 107 def persistent_objects ObjectSpace.each_object(Persistent).to_a end |
#transaction(&block) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/odb.rb', line 89 def transaction(&block) objects_before = persistent_objects self.class.transactions << self yield self.objects << (persistent_objects - objects_before) commit self.class.transactions.pop end |