Class: Snapback::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/snapback/transaction.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Transaction

Returns a new instance of Transaction.



4
5
6
7
8
9
10
11
12
13
# File 'lib/snapback/transaction.rb', line 4

def initialize(&block)
  @reverts = []

  begin
    instance_eval &block
  rescue Exception => msg  
    rollback
    raise msg # re-throw
  end
end

Instance Method Details

#revert(&block) ⇒ Object



15
16
17
# File 'lib/snapback/transaction.rb', line 15

def revert(&block)
  @reverts.push block
end

#rollbackObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/snapback/transaction.rb', line 19

def rollback
  if Snapback.verbose?
    puts ""
    puts "An error occurred ... rolling back"
    puts ""
  end

  while revert = @reverts.pop
    revert.call
  end
  
  
  puts "" if Snapback.verbose?
end