Class: Foobara::Persistence::EntityBase

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.0.110/projects/persistence/src/entity_base.rb,
foobara-0.0.110/projects/persistence/src/entity_base/table.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/state_machine.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/queries.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb,
foobara-0.0.110/projects/persistence/src/entity_base/transaction/concerns/entity_callback_handling.rb

Defined Under Namespace

Classes: Table, Transaction, TransactionTable

Constant Summary collapse

VALID_MODES =
[:use_existing, :open_nested, :open_new, nil].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, entity_attributes_crud_driver:) ⇒ EntityBase

Returns a new instance of EntityBase.



26
27
28
29
30
31
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 26

def initialize(name, entity_attributes_crud_driver:)
  self.entity_attributes_crud_driver = entity_attributes_crud_driver
  self.tables = {}
  self.name = name
  # TODO: a smell?
end

Instance Attribute Details

#entity_attributes_crud_driverObject

Returns the value of attribute entity_attributes_crud_driver.



6
7
8
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 6

def entity_attributes_crud_driver
  @entity_attributes_crud_driver
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 6

def name
  @name
end

#tablesObject

Returns the value of attribute tables.



6
7
8
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 6

def tables
  @tables
end

Class Method Details

.using_transactions(existing_transactions, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 9

def using_transactions(existing_transactions, &block)
  if existing_transactions.empty?
    block.call
  elsif existing_transactions.size == 1
    existing_transaction = existing_transactions.first

    existing_transaction.entity_base.using_transaction(existing_transaction, &block)
  else
    existing_transactions.inject(block) do |nested_proc, existing_transaction|
      proc do
        existing_transaction.entity_base.using_transaction(existing_transaction, &nested_proc)
      end
    end.call
  end
end

Instance Method Details

#current_transactionObject



47
48
49
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 47

def current_transaction
  Thread.inheritable_thread_local_var_get(transaction_key)
end

#register_entity_class(entity_class, table_name: entity_class.full_entity_name) ⇒ Object



33
34
35
36
37
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 33

def register_entity_class(entity_class, table_name: entity_class.full_entity_name)
  table = EntityBase::Table.new(table_name, self)

  register_table(table)
end

#register_table(table) ⇒ Object



39
40
41
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 39

def register_table(table)
  tables[table.table_name] = table
end

#set_current_transaction(transaction) ⇒ Object



51
52
53
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 51

def set_current_transaction(transaction)
  Thread.inheritable_thread_local_var_set(transaction_key, transaction)
end

#transaction(mode = nil, existing_transaction: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 61

def transaction(mode = nil, existing_transaction: nil)
  unless VALID_MODES.include?(mode)
    # :nocov:
    raise ArgumentError, "Mode was #{mode} but expected one of #{VALID_MODES}"
    # :nocov:
  end

  old_transaction = current_transaction

  if old_transaction&.closed?
    old_transaction = nil
  end

  if old_transaction&.currently_open?
    if mode == :use_existing || existing_transaction == old_transaction
      if block_given?
        return yield old_transaction
      else
        return old_transaction
      end
    elsif mode != :open_nested && mode != :open_new
      # :nocov:
      raise "Transaction already open. " \
            "Use mode :use_existing if you want to make use of the existing transaction. " \
            "Use mode :open_nested if you are actually trying to nest transactions."
      # :nocov:
    end
  end

  unless block_given?
    return existing_transaction || Transaction.new(self)
  end

  begin
    if existing_transaction
      tx = existing_transaction
    else
      tx = Transaction.new(self)
      tx.open!
    end

    set_current_transaction(tx)
    result = yield tx
    tx.commit! if tx.currently_open? && !existing_transaction
    result
  rescue Foobara::Persistence::EntityBase::Transaction::RolledBack # rubocop:disable Lint/SuppressedException
  rescue => e
    tx.rollback!(e) if tx.currently_open?
    raise
  ensure
    set_current_transaction(old_transaction)
  end
end

#transaction_keyObject



43
44
45
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 43

def transaction_key
  @transaction_key ||= "foobara:tx:#{name}"
end

#using_transaction(existing_transaction) ⇒ Object



57
58
59
# File 'foobara-0.0.110/projects/persistence/src/entity_base.rb', line 57

def using_transaction(existing_transaction, &)
  transaction(existing_transaction:, &)
end