Class: Foobara::Persistence::EntityAttributesCrudDriver::Table
- Inherits:
-
Object
- Object
- Foobara::Persistence::EntityAttributesCrudDriver::Table
show all
- Defined in:
- foobara-0.3.0/projects/persistence/src/entity_attributes_crud_driver.rb
Overview
TODO: relocate this to another file?
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/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.entity_name)
table_name.gsub!(/^types::/, "")
table_name.gsub!("::", "_")
end
self.crud_driver = crud_driver
self.entity_class = entity_class
self.raw_connection = crud_driver.raw_connection
self.table_name = table_name
end
|
Instance Attribute Details
#crud_driver ⇒ Object
Returns the value of attribute crud_driver.
106
107
108
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 106
def crud_driver
@crud_driver
end
|
#entity_class ⇒ Object
Returns the value of attribute entity_class.
106
107
108
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 106
def entity_class
@entity_class
end
|
#raw_connection ⇒ Object
Returns the value of attribute raw_connection.
106
107
108
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 106
def raw_connection
@raw_connection
end
|
#table_name ⇒ Object
Returns the value of attribute table_name.
106
107
108
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 106
def table_name
@table_name
end
|
Instance Method Details
#all(page_size: nil) ⇒ Object
129
130
131
132
133
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 129
def all(page_size: nil)
raise "subclass responsibility"
end
|
#all_exist?(record_ids) ⇒ Boolean
282
283
284
285
286
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 282
def all_exist?(record_ids)
record_ids.all? do |record_id|
exists?(record_id)
end
end
|
#count ⇒ Object
272
273
274
275
276
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 272
def count
raise "subclass responsibility"
end
|
#exists?(record_id) ⇒ Boolean
278
279
280
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 278
def exists?(record_id)
!!find(record_id)
end
|
#find(_record_id) ⇒ Object
139
140
141
142
143
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 139
def find(_record_id)
raise "subclass responsibility"
end
|
#find!(record_id) ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File '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/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
190
191
|
# File '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]&.any? do |attribute_value|
values.include?(attribute_value)
end
end
end
|
#find_by(attributes_filter) ⇒ Object
193
194
195
196
197
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 193
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/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/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
199
200
201
202
203
204
205
206
207
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 199
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
|
#first ⇒ Object
135
136
137
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 135
def first
all.first
end
|
#hard_delete(_record_id) ⇒ Object
251
252
253
254
255
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 251
def hard_delete(_record_id)
raise "subclass responsibility"
end
|
#hard_delete_all! ⇒ Object
266
267
268
269
270
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 266
def hard_delete_all!
raise "subclass responsibility"
end
|
#hard_delete_many(record_ids) ⇒ Object
257
258
259
260
261
262
263
264
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 257
def hard_delete_many(record_ids)
record_ids.each.lazy.map do |record_id|
delete(record_id)
end
end
|
#insert(_attributes) ⇒ Object
230
231
232
233
234
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 230
def insert(_attributes)
raise "subclass responsibility"
end
|
#insert_many(attributes_array) ⇒ Object
236
237
238
239
240
241
242
243
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 236
def insert_many(attributes_array)
attributes_array.each.lazy.map do |attributes|
insert(attributes)
end
end
|
#matches_attributes_filter?(attributes, attributes_filter) ⇒ Boolean
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 209
def matches_attributes_filter?(attributes, attributes_filter)
attributes_filter.all? do |attribute_name_or_path, value|
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_attribute ⇒ Object
292
293
294
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 292
def primary_key_attribute
entity_class.primary_key_attribute
end
|
#record_id_for(attributes) ⇒ Object
288
289
290
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 288
def record_id_for(attributes)
attributes&.[](primary_key_attribute)
end
|
#restore_attributes(object, type = entity_class.attributes_type) ⇒ Object
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
340
341
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 296
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])
object.map.with_index do |value, index|
element_type = type.element_types[index]
restore_attributes(value, element_type)
end
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
object
end
end
end
|
#select(_query_declaration) ⇒ Object
123
124
125
126
127
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 123
def select(_query_declaration)
raise "subclass responsibility"
end
|
#update(_record) ⇒ Object
245
246
247
248
249
|
# File 'projects/persistence/src/entity_attributes_crud_driver.rb', line 245
def update(_record)
raise "subclass responsibility"
end
|