Class: Yapper::DB::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/yapper/db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Transaction

Returns a new instance of Transaction.



173
174
175
# File 'lib/yapper/db.rb', line 173

def initialize(db)
  @db = db
end

Instance Attribute Details

#txnObject

Returns the value of attribute txn.



171
172
173
# File 'lib/yapper/db.rb', line 171

def txn
  @txn
end

Instance Method Details

#run(&block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/yapper/db.rb', line 177

def run(&block)
  result = nil
  txn_proc = proc do |_txn|
    @txn = _txn
    begin
      result = block.call(@txn)
    rescue Exception => e
      @txn.rollback
      result = e
    end
  end
  @db.connection.readWriteWithBlock(txn_proc)

  raise result if result.is_a?(Exception)
  result
end