Class: Transacted::Transaction

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

Instance Method Summary collapse

Constructor Details

#initialize(actions) ⇒ Transaction

Returns a new instance of Transaction.



3
4
5
6
# File 'lib/transacted/transaction.rb', line 3

def initialize actions
  raise "Actions must be of type Transacted::Action" if not are_actions? actions
  @actions = actions
end

Instance Method Details

#are_actions?(action_or_actions) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/transacted/transaction.rb', line 8

def are_actions? action_or_actions
  [*action_or_actions].all? {|action| action.is_a? Transacted::Action }
end

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/transacted/transaction.rb', line 16

def execute
  @executed_actions = []
  @actions_left = @actions.clone
  direction = :up
  status = nil
  while direction != nil do
    if direction == :up

      if @actions_left.count == 0
        direction == nil
        return :execution_success
      end

      action = @actions_left.shift

      begin
        action_value = action.up
        @executed_actions.push action
        direction = :down if action_value == false
      rescue Exception => e
        direction = :down
      end
      
    elsif direction == :down 

      if @executed_actions.count == 0
        direction == nil
        return :rollback_success
      end

      action = @executed_actions.pop

      begin
        action_value = action.down
        return :rollback_failure if action_value == false
      rescue
        return :rollback_failure
      end
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/transacted/transaction.rb', line 12

def valid?
  are_actions? @actions
end