Class: Foobara::Persistence::EntityAttributesCrudDriver::Table

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.6.1/projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb

Overview

TODO: relocate this to another file?

Direct Known Subclasses

CrudDrivers::InMemoryMinimal::Table

Defined Under Namespace

Classes: CannotCrudError, CannotDeleteError, CannotFindError, CannotInsertError, CannotUpdateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_class, crud_driver, table_name = nil) ⇒ Table

Returns a new instance of Table.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 108

def initialize(entity_class, crud_driver, table_name = nil)
  if table_name.nil?
    table_name = Util.underscore(entity_class.full_entity_name)
    table_name.gsub!(/^types::/, "")
    table_name.gsub!("::", "_")
  end

  self.crud_driver = crud_driver
  self.entity_class = entity_class
  # what is this used for?
  self.raw_connection = crud_driver.raw_connection
  self.table_name = table_name
end

Instance Attribute Details

#crud_driverObject

Returns the value of attribute crud_driver.



106
107
108
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 106

def crud_driver
  @crud_driver
end

#entity_classObject

Returns the value of attribute entity_class.



106
107
108
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 106

def entity_class
  @entity_class
end

#raw_connectionObject

Returns the value of attribute raw_connection.



106
107
108
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 106

def raw_connection
  @raw_connection
end

#table_nameObject

Returns the value of attribute table_name.



106
107
108
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 106

def table_name
  @table_name
end

Instance Method Details

#all(page_size: nil) ⇒ Object

Raises:

  • (NotImplementedError)


129
130
131
132
133
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 129

def all(page_size: nil)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#all_exist?(record_ids) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
283
284
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 280

def all_exist?(record_ids)
  record_ids.all? do |record_id|
    exists?(record_id)
  end
end

#countObject

Raises:

  • (NotImplementedError)


270
271
272
273
274
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 270

def count
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 276

def exists?(record_id)
  !!find(record_id)
end

#find(_record_id) ⇒ Object

Raises:

  • (NotImplementedError)


139
140
141
142
143
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 139

def find(_record_id)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#find!(record_id) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 145

def find!(record_id)
  attributes = find(record_id)

  unless attributes
    raise CannotFindError.new(record_id, "does not exist")
  end

  attributes
end

#find_all_by_attribute_any_of(attribute_name, values) ⇒ Object



179
180
181
182
183
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 179

def find_all_by_attribute_any_of(attribute_name, values)
  all.select do |attributes|
    values.include?(attributes[attribute_name])
  end
end

#find_all_by_attribute_containing_any_of(attribute_name, values) ⇒ Object



185
186
187
188
189
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 185

def find_all_by_attribute_containing_any_of(attribute_name, values)
  all.select do |attributes|
    attributes[attribute_name]&.intersect?(values)
  end
end

#find_by(attributes_filter) ⇒ Object



191
192
193
194
195
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 191

def find_by(attributes_filter)
  all.find do |found_attributes|
    matches_attributes_filter?(found_attributes, attributes_filter)
  end
end

#find_by_attribute_containing(attribute_name, value) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 161

def find_by_attribute_containing(attribute_name, value)
  found_type = entity_class.attributes_type.type_at_path("#{attribute_name}.#")

  if value
    value = restore_attributes(value, found_type)
  end

  all.find do |found_attributes|
    found_attributes[attribute_name].any? do |found_value|
      if found_value
        found_value = restore_attributes(found_value, found_type)
      end

      found_value == value
    end
  end
end

#find_many!(record_ids) ⇒ Object



155
156
157
158
159
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 155

def find_many!(record_ids)
  record_ids.each.lazy.map do |record_id|
    find!(record_id)
  end
end

#find_many_by(attributes_filter) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 197

def find_many_by(attributes_filter)
  Enumerator.new do |yielder|
    all.each do |found_attributes|
      if matches_attributes_filter?(found_attributes, attributes_filter)
        yielder << found_attributes
      end
    end
  end
end

#firstObject



135
136
137
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 135

def first
  all.first
end

#hard_delete(_record_id) ⇒ Object

Raises:

  • (NotImplementedError)


249
250
251
252
253
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 249

def hard_delete(_record_id)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#hard_delete_all!Object

Raises:

  • (NotImplementedError)


264
265
266
267
268
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 264

def hard_delete_all!
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#hard_delete_many(record_ids) ⇒ Object



255
256
257
258
259
260
261
262
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 255

def hard_delete_many(record_ids)
  # TODO: add a test for a driver that doesn't override this and remove these :nocov: comments
  # simplecov:disable
  record_ids.each.lazy.map do |record_id|
    delete(record_id)
  end
  # simplecov:enable
end

#insert(_attributes) ⇒ Object

Raises:

  • (NotImplementedError)


228
229
230
231
232
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 228

def insert(_attributes)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#insert_many(attributes_array) ⇒ Object



234
235
236
237
238
239
240
241
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 234

def insert_many(attributes_array)
  # TODO: add a test for a driver that doesn't override this and remove these :nocov: comments
  # simplecov:disable
  attributes_array.each.lazy.map do |attributes|
    insert(attributes)
  end
  # simplecov:enable
end

#matches_attributes_filter?(attributes, attributes_filter) ⇒ Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 207

def matches_attributes_filter?(attributes, attributes_filter)
  attributes_filter.all? do |attribute_name_or_path, value|
    # get the model-free type?
    attribute_type = entity_class.attributes_type.type_at_path(attribute_name_or_path)

    value = restore_attributes(value, attribute_type)

    if attribute_name_or_path.is_a?(::Array)
      values = DataPath.values_at(attribute_name_or_path, attributes)

      values.any? do |attribute_value|
        restore_attributes(attribute_value, attribute_type) == value
      end
    else
      attribute_value = DataPath.value_at(attribute_name_or_path, attributes)
      attribute_value = restore_attributes(attribute_value, attribute_type)
      attribute_value == value
    end
  end
end

#primary_key_attributeObject



290
291
292
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 290

def primary_key_attribute
  entity_class.primary_key_attribute
end

#record_id_for(attributes) ⇒ Object



286
287
288
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 286

def record_id_for(attributes)
  attributes&.[](primary_key_attribute)
end

#restore_attributes(object, type = entity_class.attributes_type) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 294

def restore_attributes(object, type = entity_class.attributes_type)
  if type.extends?(BuiltinTypes[:attributes])
    object.to_h do |attribute_name, attribute_value|
      attribute_type = type.type_at_path(attribute_name)
      [attribute_name.to_sym, restore_attributes(attribute_value, attribute_type)]
    end
  elsif type.extends?(BuiltinTypes[:tuple])
    # TODO: test this code path
    # simplecov:disable
    object.map.with_index do |value, index|
      element_type = type.element_types[index]
      restore_attributes(value, element_type)
    end
    # simplecov:enable
  elsif type.extends?(BuiltinTypes[:array])
    element_type = type.element_type
    object.map { |value| restore_attributes(value, element_type) }
  elsif type.extends?(BuiltinTypes[:entity])
    if object.is_a?(Model)
      if object.persisted?
        object = object.primary_key
        restore_attributes(object, type.target_class.primary_key_type)
      else
        object
      end
    else
      restore_attributes(object, type.target_class.primary_key_type)
    end
  elsif type.extends?(BuiltinTypes[:model])
    if object.is_a?(Model)
      object = object.attributes
    end
    restore_attributes(object, type.element_types)
  else
    outcome = type.process_value(object)

    if outcome.success?
      outcome.result
    else
      # TODO: figure out how to test this code path
      # simplecov:disable
      object
      # simplecov:enable
    end
  end
end

#select(_query_declaration) ⇒ Object

CRUD

Raises:

  • (NotImplementedError)


123
124
125
126
127
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 123

def select(_query_declaration)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end

#update(_record) ⇒ Object

Raises:

  • (NotImplementedError)


243
244
245
246
247
# File 'projects/entities/projects/persistence/src/entity_attributes_crud_driver.rb', line 243

def update(_record)
  # simplecov:disable
  raise NotImplementedError
  # simplecov:enable
end