Module: Foobara::Persistence::EntityBase::Transaction::Concerns::StateTransitions

Defined in:
foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb

Instance Method Summary collapse

Instance Method Details

#commit!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 57

def commit!
  return commit_nested! if nested?

  state_machine.commit! do
    each_table(&:commit!)
    entity_attributes_crud_driver.commit_transaction(raw_tx)
    each_table(&:transaction_closed)
  end
rescue => e
  # :nocov:
  rollback!(e)
  raise
  # :nocov:
end

#commit_nested!Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 72

def commit_nested!
  state_machine.commit_nested! do
    each_table(&:commit!)
    entity_attributes_crud_driver.flush_transaction(raw_tx)
    each_table(&:transaction_closed)
  end
rescue => e
  # :nocov:
  rollback!(e)
  raise
  # :nocov:
end

#currently_open?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 12

def currently_open?
  state_machine.currently_open?
end

#each_tableObject

TODO: this belongs elsewhere



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 86

def each_table(&)
  @ordered_tables ||= if tables.size <= 1
                        tables.values
                      else
                        entity_class_to_table = {}
                        entity_classes = []

                        tables.each_value do |table|
                          entity_class = table.entity_class
                          entity_classes << entity_class
                          entity_class_to_table[entity_class] = table
                        end

                        ordered_entity_classes = EntityBase.order_entity_classes(entity_classes)

                        ordered_entity_classes.map do |entity_class|
                          entity_class_to_table[entity_class]
                        end
                      end

  @ordered_tables.each(&)
end

#flush!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 29

def flush!
  state_machine.flush! do
    each_table(&:validate!)
    each_table(&:flush_created!)
    each_table(&:flush_updated_and_hard_deleted!)
  end
  entity_attributes_crud_driver.flush_transaction(raw_tx)
rescue => e
  # :nocov:
  rollback!(e)
  raise
  # :nocov:
end

#open!Object



16
17
18
19
20
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 16

def open!
  state_machine.open! do
    self.raw_tx = entity_attributes_crud_driver.open_transaction
  end
end

#open_nested!(outer_tx) ⇒ Object



22
23
24
25
26
27
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 22

def open_nested!(outer_tx)
  state_machine.open_nested! do
    self.is_nested = true
    self.raw_tx = outer_tx.raw_tx
  end
end

#revert!Object

Should communicate somehow that this is only in-memory. TODO: support multiple-reverts for databases that support checkpoints



45
46
47
48
49
50
51
52
53
54
55
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 45

def revert!
  state_machine.revert! do
    each_table(&:revert!)
  end
  entity_attributes_crud_driver.revert_transaction(raw_tx)
rescue => e
  # :nocov:
  rollback!(e)
  raise
  # :nocov:
end

#rollback!(because_of = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 109

def rollback!(because_of = nil)
  return rollback_nested!(because_of) if nested?

  state_machine.rollback! do
    # TODO: raise error if already flushed and if crud_driver doesn't support true transactions
    entity_attributes_crud_driver.rollback_transaction(raw_tx)
    each_table(&:rollback!)
  end

  each_table(&:transaction_closed)

  if !because_of && (self == entity_base.current_transaction)
    raise RolledBack, "intentionally rolled back"
  end
rescue
  state_machine.error! if state_machine.currently_open?
  raise
end

#rollback_nested!(because_of = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb', line 128

def rollback_nested!(because_of = nil)
  state_machine.rollback_nested! do
    entity_attributes_crud_driver.revert_transaction(raw_tx)
    each_table(&:revert!)
  end

  each_table(&:transaction_closed)

  if !because_of && (self == entity_base.current_transaction)
    raise RolledBack, "intentionally rolled back"
  end
rescue
  state_machine.error! if state_machine.currently_open?
  raise
end