Module: Foobara::Persistence

Defined in:
foobara-0.2.2/projects/persistence/src/entity_attributes_crud_driver.rb,
foobara-0.2.2/projects/persistence/src/entity_base.rb,
foobara-0.2.2/projects/persistence/src/persistence.rb,
foobara-0.2.2/projects/persistence/src/entity_base/table.rb,
foobara-0.2.2/projects/in_memory_crud_driver/src/in_memory.rb,
foobara-0.2.2/projects/persistence/lib/foobara/persistence.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction_table.rb,
foobara-0.2.2/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction/state_machine.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction_table/concerns/queries.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction/concerns/state_transitions.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction_table/concerns/record_tracking.rb,
foobara-0.2.2/projects/persistence/src/entity_base/transaction/concerns/entity_callback_handling.rb

Overview

Might be best to rename this to CrudDrivers or CrudDriver instead of Persistence?

Defined Under Namespace

Modules: CrudDrivers Classes: EntityAttributesCrudDriver, EntityBase, InvalidRecordError, NoTableOrCrudDriverError, NoTransactionOpenError

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_crud_driverObject

Returns the value of attribute default_crud_driver.



7
8
9
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 7

def default_crud_driver
  @default_crud_driver
end

Class Method Details

.base_for_entity_class(entity_class) ⇒ Object



131
132
133
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 131

def base_for_entity_class(entity_class)
  table_for_entity_class(entity_class).entity_base
end

.basesObject



127
128
129
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 127

def bases
  @bases ||= {}
end

.bases_need_sorting!Object



261
262
263
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 261

def bases_need_sorting!
  @bases_need_sorting = true
end

.bases_need_sorting?Boolean

Returns:

  • (Boolean)


265
266
267
268
269
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 265

def bases_need_sorting?
  return true if @bases_need_sorting

  @bases_need_sorting = table_count != last_table_count
end

.current_transaction(object) ⇒ Object

TODO: support transactions across multiple bases



48
49
50
51
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 48

def current_transaction(object)
  base = to_base(object)
  base.current_transaction
end

.current_transaction!(object) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 53

def current_transaction!(object)
  base = to_base(object)
  tx = base.current_transaction

  unless tx
    # :nocov:
    raise NoTransactionOpenError
    # :nocov:
  end

  tx
end

.current_transaction_table(object) ⇒ Object



70
71
72
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 70

def current_transaction_table(object)
  current_transaction(object).table_for(object)
end

.current_transaction_table!(object) ⇒ Object



66
67
68
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 66

def current_transaction_table!(object)
  current_transaction!(object).table_for(object)
end

.default_baseObject



19
20
21
22
23
24
25
26
27
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 19

def default_base
  @default_base ||= if default_crud_driver
                      base = EntityBase.new(
                        "default_entity_base",
                        entity_attributes_crud_driver: default_crud_driver
                      )
                      register_base(base)
                    end
end

.install!Object



17
18
19
20
# File 'foobara-0.2.2/projects/persistence/lib/foobara/persistence.rb', line 17

def install!
  EntityBase::Transaction::Concerns::EntityCallbackHandling.install!
  EntityBase::Transaction.install!
end

.object_to_base(object) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 108

def object_to_base(object)
  case object
  when EntityBase
    object
  when ::String
    bases[object]
  when ::Symbol
    bases[object.to_s]
  when Class
    base_for_entity_class(object)
  when Entity
    base_for_entity_class(object.class)
  else
    # :nocov:
    raise ArgumentError, "Not able to convert #{object} to an entity base"
    # :nocov:
  end
end

.objects_to_bases(objects) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 96

def objects_to_bases(objects)
  unsorted = objects.map do |object|
    object_to_base(object)
  end.uniq

  if bases_need_sorting?
    sort_bases!
  end

  bases.values & unsorted
end

.register_base(*args, name: nil, table_prefix: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 157

def register_base(*args, name: nil, table_prefix: nil)
  base = case args
         in [EntityBase]
           args.first
         in [Class => crud_driver_class, *rest] if crud_driver_class < EntityAttributesCrudDriver
           unless name
             # :nocov:
             raise ArgumentError, "Must provide name: when registering a base with a crud driver class"
             # :nocov:
           end

           crud_args, opts = case rest
                             in [Hash]
                               # TODO: test this code path
                               # :nocov:
                               [[], rest]
                               # :nocov:
                             in [] | [Array] | [Array, Hash]
                               rest
                             end

           crud_driver = crud_driver_class.new(*crud_args, **opts, table_prefix:)
           EntityBase.new(name, entity_attributes_crud_driver: crud_driver)
         end

  bases_need_sorting!
  bases[base.name] = base
end

.register_entity(base, entity_class, table_name: entity_class.full_entity_name) ⇒ Object



271
272
273
274
275
276
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 271

def register_entity(base, entity_class, table_name: entity_class.full_entity_name)
  base = to_base(base)

  table = base.register_entity_class(entity_class, table_name:)
  tables_for_entity_class_name[entity_class.full_entity_name] = table
end

.reset_allObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'foobara-0.2.2/projects/persistence/lib/foobara/persistence.rb', line 4

def reset_all
  Foobara.raise_if_production!("reset_all")
  @tables_for_entity_class_name = @bases = @default_crud_driver = @default_base = nil
  EntityBase::Transaction::Concerns::EntityCallbackHandling.reset_all
  EntityBase::Transaction.reset_all

  Util.descendants(Foobara::Entity).each do |entity_class|
    if entity_class.instance_variable_defined?(:@entity_base)
      entity_class.remove_instance_variable(:@entity_base)
    end
  end
end

.sort_bases(bases) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 186

def sort_bases(bases)
  return bases.dup if bases.size <= 1

  if bases_need_sorting?
    sort_bases!
  end

  sorted_bases = self.bases.values & bases

  missing = bases - sorted_bases

  unless missing.empty?
    # :nocov:
    raise ArgumentError, "Missing bases: #{missing} are the not registered or something?"
    # :nocov:
    # sorted_bases = [*missing, *sorted_bases]
  end

  sorted_bases
end

.sort_bases!Object

TODO: make this private TODO: add a callback so objects that are sensitive to this order can update when needed



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 223

def sort_bases!
  return if bases.size <= 1

  old_bases = bases.values

  entity_classes = []

  old_bases.each do |base|
    entity_classes += base.entity_classes
  end

  return if entity_classes.size <= 1

  entity_classes.select!(&:contains_associations?)

  return if entity_classes.size <= 1

  entity_classes = EntityBase.order_entity_classes(entity_classes)

  new_bases = entity_classes.map(&:entity_base)
  new_bases.reverse!
  new_bases.uniq!

  missing = old_bases - new_bases

  unless missing.empty?
    new_bases = [*new_bases, *missing]
  end

  @bases_need_sorting = false

  self.last_table_count = table_count

  @bases = new_bases.to_h do |base|
    [base.name, base]
  end
end

.sort_transactions(transactions) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 207

def sort_transactions(transactions)
  return transactions.dup if transactions.size <= 1

  if bases_need_sorting?
    sort_bases!
  end

  sorted_bases = bases.values & transactions.map(&:entity_base)

  sorted_bases.map do |base|
    transactions.find { |tx| tx.entity_base == base }
  end
end

.table_for_entity_class(entity_class) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 135

def table_for_entity_class(entity_class)
  entity_class_name = entity_class.full_entity_name
  table = tables_for_entity_class_name[entity_class_name]

  return table if table

  domain = entity_class.domain

  base = domain.foobara_default_entity_base || default_base

  if base
    table = EntityBase::Table.new(entity_class_name, base)
    base.register_table(table)
    tables_for_entity_class_name[entity_class_name] = table
  else
    # :nocov:
    raise NoTableOrCrudDriverError,
          "Can't find table for #{entity_class_name} and can't dynamically build one without default crud driver."
    # :nocov:
  end
end

.tables_for_entity_class_nameObject



278
279
280
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 278

def tables_for_entity_class_name
  @tables_for_entity_class_name ||= {}
end

.to_base(object) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 74

def to_base(object)
  bases = to_bases(object)

  if bases.empty?
    # :nocov:
    raise "Could not find a base for #{object}"
    # :nocov:
  end

  if bases.size > 1
    # :nocov:
    raise "Expected to only find 1 base for #{object} but found #{bases.size}"
    # :nocov:
  end

  bases.first
end

.to_bases(object) ⇒ Object



92
93
94
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 92

def to_bases(object)
  objects_to_bases(Util.array(object))
end

.transaction(*objects, mode: nil) ⇒ Object

TODO: automatically order these by dependency… TODO: also, consider automatically opening transactions for dependent entities automatically…



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'foobara-0.2.2/projects/persistence/src/persistence.rb', line 31

def transaction(*objects, mode: nil, &)
  bases = objects_to_bases(objects)

  if bases.empty?
    # :nocov:
    raise "No bases found for #{objects}"
    # :nocov:
  end

  if bases.size == 1
    bases.first.transaction(mode, &)
  else
    Foobara::TransactionGroup.run(bases:, mode:, &)
  end
end