Module: Foobara::CommandPatternImplementation::Concerns::Transactions

Includes:
Foobara::Concern
Included in:
Foobara::CommandPatternImplementation
Defined in:
foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb

Instance Method Summary collapse

Methods included from Foobara::Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Method Details

#auto_detect_current_transactionsObject



15
16
17
18
19
20
21
22
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 15

def auto_detect_current_transactions
  bases = relevant_entity_classes.map(&:entity_base).uniq

  bases.each do |base|
    tx = base.current_transaction
    transactions << tx if tx
  end
end

#commit_transactionObject



79
80
81
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 79

def commit_transaction
  opened_transactions.reverse.each(&:commit!)
end

#open_transactionObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 52

def open_transaction
  auto_detect_current_transactions

  bases_not_needing_transaction = transactions.map(&:entity_base)

  bases_needing_transaction = relevant_entity_classes.map(&:entity_base).uniq - bases_not_needing_transaction

  bases_needing_transaction.each do |entity_base|
    transaction = entity_base.transaction
    transaction.open!
    opened_transactions << transaction
    transactions << transaction
  end
end

#opened_transactionsObject



11
12
13
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 11

def opened_transactions
  @opened_transactions ||= []
end

#relevant_entity_classesObject



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
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 24

def relevant_entity_classes
  @relevant_entity_classes ||= begin
    entity_classes = if inputs_type
                       Entity.construct_associations(
                         inputs_type
                       ).values.uniq.map(&:target_class)
                     else
                       []
                     end

    if result_type
      entity_classes += Entity.construct_associations(
        result_type
      ).values.uniq.map(&:target_class)

      if result_type.extends?(BuiltinTypes[:entity])
        entity_classes << result_type.target_class
      end
    end

    entity_classes += entity_classes.uniq.map do |entity_class|
      entity_class.deep_associations.values
    end.flatten.uniq.map(&:target_class)

    [*entity_classes, *self.class.depends_on_entities].uniq
  end
end

#rollback_transactionObject



67
68
69
70
71
72
73
74
75
76
77
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 67

def rollback_transaction
  opened_transactions.reverse.each do |transaction|
    if transaction.currently_open?
      # Hard to test this because halting and other exceptions rollback the transactions via
      # block form but to be safe keeping this
      # :nocov:
      transaction.rollback!
      # :nocov:
    end
  end
end

#transactionsObject



7
8
9
# File 'foobara-0.0.110/projects/command/src/command_pattern_implementation/concerns/transactions.rb', line 7

def transactions
  @transactions ||= []
end