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

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.1.7/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 = Util.underscore(entity_class.entity_name)) ⇒ Table

Returns a new instance of Table.



85
86
87
88
89
90
91
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 85

def initialize(entity_class, crud_driver, table_name = Util.underscore(entity_class.entity_name))
  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.



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

def crud_driver
  @crud_driver
end

#entity_classObject

Returns the value of attribute entity_class.



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

def entity_class
  @entity_class
end

#raw_connectionObject

Returns the value of attribute raw_connection.



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

def raw_connection
  @raw_connection
end

#table_nameObject

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#all(page_size: nil) ⇒ Object



100
101
102
103
104
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 100

def all(page_size: nil)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#all_exist?(record_ids) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 253

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

#countObject



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

def count
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 249

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

#find(_record_id) ⇒ Object



110
111
112
113
114
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 110

def find(_record_id)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#find!(record_id) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 116

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



150
151
152
153
154
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 150

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



156
157
158
159
160
161
162
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 156

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

#find_by(attributes_filter) ⇒ Object



164
165
166
167
168
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 164

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 132

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



126
127
128
129
130
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 126

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

#find_many_by(attributes_filter) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 170

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



106
107
108
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 106

def first
  all.first
end

#hard_delete(_record_id) ⇒ Object



222
223
224
225
226
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 222

def hard_delete(_record_id)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#hard_delete_all!Object



237
238
239
240
241
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 237

def hard_delete_all!
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#hard_delete_many(record_ids) ⇒ Object



228
229
230
231
232
233
234
235
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 228

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

#insert(_attributes) ⇒ Object



201
202
203
204
205
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 201

def insert(_attributes)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#insert_many(attributes_array) ⇒ Object



207
208
209
210
211
212
213
214
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 207

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

#matches_attributes_filter?(attributes, attributes_filter) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 180

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



263
264
265
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 263

def primary_key_attribute
  entity_class.primary_key_attribute
end

#record_id_for(attributes) ⇒ Object



259
260
261
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 259

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

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 267

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
    # :nocov:
    object.map.with_index do |value, index|
      element_type = type.element_types[index]
      restore_attributes(value, element_type)
    end
    # :nocov:
  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
      # :nocov:
      object
      # :nocov:
    end
  end
end

#select(_query_declaration) ⇒ Object

CRUD



94
95
96
97
98
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 94

def select(_query_declaration)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#update(_record) ⇒ Object



216
217
218
219
220
# File 'foobara-0.1.7/projects/persistence/src/entity_attributes_crud_driver.rb', line 216

def update(_record)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end