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

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


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

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

#countObject



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

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

#exists?(record_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#find(_record_id) ⇒ Object



104
105
106
107
108
# File 'foobara-0.0.130/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
115
116
117
118
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 110

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



132
133
134
135
136
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 132

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



138
139
140
141
142
143
144
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 138

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



146
147
148
149
150
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 146

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



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

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



120
121
122
123
124
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 120

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

#find_many_by(attributes_filter) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'foobara-0.0.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 152

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.130/projects/persistence/src/entity_attributes_crud_driver.rb', line 100

def first
  all.first
end

#hard_delete(_record_id) ⇒ Object



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

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

#hard_delete_all!Object



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

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

#hard_delete_many(record_ids) ⇒ Object



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

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



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

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

#insert_many(attributes_array) ⇒ Object



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

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)


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

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



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

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



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

def primary_key_attribute
  entity_class.primary_key_attribute
end

#record_id_for(attributes) ⇒ Object



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

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

#select(_query_declaration) ⇒ Object

CRUD



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

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

#update(_record) ⇒ Object



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

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