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

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.0.110/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.



79
80
81
82
83
84
85
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 79

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.



77
78
79
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 77

def crud_driver
  @crud_driver
end

#entity_classObject

Returns the value of attribute entity_class.



77
78
79
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 77

def entity_class
  @entity_class
end

#raw_connectionObject

Returns the value of attribute raw_connection.



77
78
79
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 77

def raw_connection
  @raw_connection
end

#table_nameObject

Returns the value of attribute table_name.



77
78
79
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 77

def table_name
  @table_name
end

Instance Method Details

#allObject



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

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

#all_exist?(record_ids) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#countObject



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

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

#exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find(_record_id) ⇒ Object



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

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

#find!(_record_id) ⇒ Object



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

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

#find_all_by_attribute_any_of(attribute_name, values) ⇒ Object



128
129
130
131
132
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 128

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



134
135
136
137
138
139
140
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 134

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



142
143
144
145
146
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 142

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



122
123
124
125
126
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 122

def find_by_attribute_containing(attribute_name, value)
  all.find do |found_attributes|
    found_attributes[attribute_name]&.include?(value)
  end
end

#find_many!(record_ids) ⇒ Object



116
117
118
119
120
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 116

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

#find_many_by(attributes_filter) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 148

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



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

def first
  all.first
end

#hard_delete(_record_id) ⇒ Object



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

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

#hard_delete_all!Object



232
233
234
235
236
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 232

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

#hard_delete_many(record_ids) ⇒ Object



223
224
225
226
227
228
229
230
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 223

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



196
197
198
199
200
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 196

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

#insert_many(attributes_array) ⇒ Object



202
203
204
205
206
207
208
209
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 202

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)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 158

def matches_attributes_filter?(attributes, attributes_filter)
  attributes_filter.all? do |attribute_name_or_path, value|
    value = normalize_attribute_filter_value(value)

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

      values.any? do |attribute_value|
        normalize_attribute_filter_value(attribute_value) == value
      end
    else
      attribute_value = attributes[attribute_name_or_path]
      normalize_attribute_filter_value(attribute_value) == value
    end
  end
end

#normalize_attribute_filter_value(value) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 175

def normalize_attribute_filter_value(value)
  case value
  when ::Array
    value.map { |v| normalize_attribute_filter_value(v) }
  when ::Hash
    value.to_h do |k, v|
      [normalize_attribute_filter_value(k), normalize_attribute_filter_value(v)]
    end
  when DetachedEntity
    if value.persisted?
      normalize_attribute_filter_value(value.primary_key)
    else
      value
    end
  when Model
    normalize_attribute_filter_value(value.attributes)
  else
    value
  end
end

#primary_key_attributeObject



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

def primary_key_attribute
  entity_class.primary_key_attribute
end

#record_id_for(attributes) ⇒ Object



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

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

#select(_query_declaration) ⇒ Object

CRUD



88
89
90
91
92
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 88

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

#update(_record) ⇒ Object



211
212
213
214
215
# File 'foobara-0.0.110/projects/persistence/src/entity_attributes_crud_driver.rb', line 211

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