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.



230
231
232
# File 'lib/yapper/db.rb', line 230

def initialize(db)
  @db = db
end

Instance Attribute Details

#txnObject

Returns the value of attribute txn.



228
229
230
# File 'lib/yapper/db.rb', line 228

def txn
  @txn
end

Instance Method Details

#run(&block) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/yapper/db.rb', line 234

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