Class: UpAndAtThem::Transaction

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

Instance Method Summary collapse

Constructor Details

#initialize(tasks) ⇒ Transaction

Returns a new instance of Transaction.



23
24
25
26
27
28
29
30
# File 'lib/up_and_at_them.rb', line 23

def initialize(tasks)
  tasks = Array(tasks)
  tasks.each do |t|
    Commit === t or raise TypeError
  end
  @tasks = tasks
  run
end

Instance Method Details

#runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/up_and_at_them.rb', line 32

def run
  finished_tasks = []
  begin
    @tasks.each do |task|
      task.call
      finished_tasks << task
    end
  rescue => err
    finished_tasks.reverse_each do |task|
      task.rollback
    end
    raise err
  end
end