Class: ODB::Transaction

Inherits:
Object show all
Defined in:
lib/odb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#objectsObject

Returns the value of attribute objects.



73
74
75
# File 'lib/odb.rb', line 73

def objects
  @objects
end

Class Method Details

.currentObject



79
80
81
# File 'lib/odb.rb', line 79

def self.current
  transactions.last
end

.transactionsObject



75
76
77
# File 'lib/odb.rb', line 75

def self.transactions
  Thread.current['__odb_transactions'] ||= []
end

Instance Method Details

#commitObject



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_objectsObject



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