Class: Foobara::Persistence::EntityBase::Transaction

Inherits:
Object
  • Object
show all
Includes:
Concerns::EntityCallbackHandling, Concerns::StateTransitions, Concerns::TransactionTracking
Defined in:
foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb,
foobara-0.1.7/projects/persistence/src/entity_base/transaction/state_machine.rb,
foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb,
foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb,
foobara-0.1.7/projects/persistence/src/entity_base/transaction/concerns/entity_callback_handling.rb

Defined Under Namespace

Modules: Concerns Classes: CurrentTransactionIsClosedError, NoCurrentTransactionError, RolledBack, StateMachine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_base) ⇒ Transaction

Returns a new instance of Transaction.



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

def initialize(entity_base)
  self.entity_base = entity_base
  self.state_machine = StateMachine.new(owner: self)
  self.tables = {}
end

Instance Attribute Details

#entity_baseObject

Returns the value of attribute entity_base.



9
10
11
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 9

def entity_base
  @entity_base
end

#is_nestedObject

Returns the value of attribute is_nested.



9
10
11
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 9

def is_nested
  @is_nested
end

#raw_txObject

Returns the value of attribute raw_tx.



9
10
11
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 9

def raw_tx
  @raw_tx
end

#state_machineObject

Returns the value of attribute state_machine.



9
10
11
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 9

def state_machine
  @state_machine
end

#tablesObject

Returns the value of attribute tables.



9
10
11
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 9

def tables
  @tables
end

Instance Method Details

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  state_machine.currently_closed?
end

#create(entity_class, attributes = {}) ⇒ Object



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

def create(entity_class, attributes = {})
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.create(attributes)
  end
end

#entity_attributes_crud_driverObject



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

def entity_attributes_crud_driver
  entity_base.entity_attributes_crud_driver
end

#hard_delete_all!(entity_class) ⇒ Object



99
100
101
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 99

def hard_delete_all!(entity_class)
  table_for(entity_class).hard_delete_all!
end

#hard_deleted(record) ⇒ Object



87
88
89
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 87

def hard_deleted(record)
  table_for(record).hard_deleted(record)
end

#load(record_or_entity_class, record_id = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 103

def load(record_or_entity_class, record_id = nil)
  entity_or_id = if record_or_entity_class.is_a?(Entity)
                   if record_id
                     # :nocov:
                     raise ArgumentError, "Do not give a record_id when also giving a record"
                     # :nocov:
                   end

                   record_or_entity_class
                 else
                   unless record_id
                     # :nocov:
                     raise ArgumentError, "Must give a record_id when passing in an entity class"
                     # :nocov:
                   end

                   record_id
                 end

  table_for(record_or_entity_class).load(entity_or_id)
end

#load_aggregate(record) ⇒ Object



151
152
153
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 151

def load_aggregate(record)
  load_aggregates([record]).first
end

#load_aggregates(to_load_records) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 125

def load_aggregates(to_load_records)
  to_load = Set.new

  to_load_records.group_by(&:class).each_pair do |entity_class, records|
    unloaded = records.select { |record| record.persisted? && !record.loaded? }
    entity_class.current_transaction_table.load_many(unloaded)

    associations = entity_class.associations

    records.each do |record|
      associations.each_key do |data_path|
        # TODO: is there a more performant way to append an array to a set? probably.
        Foobara::DataPath.values_at(data_path, record).each do |value|
          to_load << value
        end
      end
    end
  end

  unless to_load.empty?
    load_aggregates(to_load)
  end

  to_load_records
end

#loaded(entity_class, attributes) ⇒ Object



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

def loaded(entity_class, attributes)
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.loaded(attributes)
  end
end

#loading?(record) ⇒ Boolean

Returns:

  • (Boolean)


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

def loading?(record)
  table_for(record).loading?(record)
end

#nested?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 171

def nested?
  is_nested
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  state_machine.currently_open?
end

#performObject



167
168
169
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 167

def perform(&)
  entity_base.using_transaction(self, &)
end

#table_for(entity_class) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 55

def table_for(entity_class)
  if entity_class.is_a?(Entity)
    entity_class = entity_class.class
  end

  # TODO: so much passing self around...
  unless entity_base == entity_class.entity_base
    # :nocov:
    raise "#{entity_class} is from a different entity base! Cannot proceed."
    # :nocov:
  end

  table = tables[entity_class]

  if table
    table
  else
    if defined?(@ordered_tables)
      # TODO: test this path
      # :nocov:
      remove_instance_variable(:@ordered_tables)
      # :nocov:
    end

    tables[entity_class] = TransactionTable.new(self, entity_class)
  end
end

#thunk(entity_class, record_id) ⇒ Object



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

def thunk(entity_class, record_id)
  Persistence.to_base(entity_class).transaction(existing_transaction: self) do
    entity_class.thunk(record_id)
  end
end

#track_created(entity) ⇒ Object



163
164
165
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 163

def track_created(entity)
  table_for(entity).track_created(entity)
end

#track_loaded(entity) ⇒ Object



159
160
161
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 159

def track_loaded(entity)
  table_for(entity).track_loaded(entity)
end

#track_unloaded_thunk(entity) ⇒ Object



155
156
157
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 155

def track_unloaded_thunk(entity)
  table_for(entity).track_unloaded_thunk(entity)
end

#tracking?(record) ⇒ Boolean

Returns:

  • (Boolean)


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

def tracking?(record)
  table_for(record).tracking?(record)
end

#truncate!Object



95
96
97
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 95

def truncate!
  each_table(&:hard_delete_all!)
end

#unhard_deleted(record) ⇒ Object



91
92
93
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 91

def unhard_deleted(record)
  table_for(record).unhard_deleted(record)
end

#updated(record) ⇒ Object



83
84
85
# File 'foobara-0.1.7/projects/persistence/src/entity_base/transaction.rb', line 83

def updated(record)
  table_for(record).updated(record)
end