Module: Familia::Features::Relationships::Indexing::ModelInstanceMethods

Defined in:
lib/familia/features/relationships/indexing.rb

Overview

Instance methods for indexed objects

Instance Method Summary collapse

Instance Method Details

#current_indexingsArray<Hash>

Get all indexes this object appears in Note: For instance-scoped indexes, this only shows class-level indexes since instance-scoped indexes require a specific scope instance

Returns:

  • (Array<Hash>)

    Array of index information



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/familia/features/relationships/indexing.rb', line 203

def current_indexings
  return [] unless self.class.respond_to?(:indexing_relationships)

  memberships = []

  self.class.indexing_relationships.each do |config|
    field = config.field
    index_name = config.index_name
    cardinality = config.cardinality
    field_value = send(field)

    next unless field_value

    if config.within.nil?
      # Class-level index (unique_index without within:) - check hash key using DataType
      index_hash = self.class.send(index_name)
      next unless index_hash.key?(field_value.to_s)

      memberships << {
        scope_class: 'class',
        index_name: index_name,
        field: field,
        field_value: field_value,
        index_key: index_hash.dbkey,
        cardinality: cardinality,
        type: 'unique_index',
      }
    else
      # Instance-scoped index (unique_index or multi_index with within:) - cannot check without scope instance
      # This would require scanning all possible scope instances
      memberships << {
        scope_class: config.scope_class_config_name,
        index_name: index_name,
        field: field,
        field_value: field_value,
        index_key: 'scope_dependent',
        cardinality: cardinality,
        type: cardinality == :unique ? 'unique_index' : 'multi_index',
        note: 'Requires scope instance for verification',
      }
    end
  end

  memberships
end

#indexed_in?(index_name) ⇒ Boolean

Check if this object is indexed in a specific scope For class-level indexes, checks the hash key For instance-scoped indexes, returns false (requires scope instance)

Returns:

  • (Boolean)


252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/familia/features/relationships/indexing.rb', line 252

def indexed_in?(index_name)
  return false unless self.class.respond_to?(:indexing_relationships)

  config = self.class.indexing_relationships.find { |rel| rel.index_name == index_name }
  return false unless config

  field = config.field
  field_value = send(field)
  return false unless field_value

  if config.within.nil?
    # Class-level index (class_indexed_by) - check hash key using DataType
    index_hash = self.class.send(index_name)
    index_hash.key?(field_value.to_s)
  else
    # Instance-scoped index (with within:) - cannot verify without scope instance
    false
  end
end

#remove_from_all_indexes(scope_context = nil) ⇒ Object

Remove from all indexes for a given scope context For class-level indexes (unique_index without within:), scope_context should be nil For instance-scoped indexes (with within:), scope_context should be the scope instance



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/familia/features/relationships/indexing.rb', line 177

def remove_from_all_indexes(scope_context = nil)
  return unless self.class.respond_to?(:indexing_relationships)

  self.class.indexing_relationships.each do |config|
    index_name = config.index_name

    # Determine which remove method to call
    if config.within.nil?
      # Class-level index (unique_index without within:)
      send("remove_from_class_#{index_name}")
    else
      # Instance-scoped index (unique_index or multi_index with within:) - requires scope context
      next unless scope_context

      # Use config_name for method naming
      scope_class_config = Familia.resolve_class(config.scope_class).config_name
      send("remove_from_#{scope_class_config}_#{index_name}", scope_context)
    end
  end
end

#update_all_indexes(old_values = {}, scope_context = nil) ⇒ Object

Update all indexes for a given scope context For class-level indexes (unique_index without within:), scope_context should be nil For instance-scoped indexes (with within:), scope_context should be the scope instance



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/familia/features/relationships/indexing.rb', line 151

def update_all_indexes(old_values = {}, scope_context = nil)
  return unless self.class.respond_to?(:indexing_relationships)

  self.class.indexing_relationships.each do |config|
    field = config.field
    index_name = config.index_name
    old_field_value = old_values[field]

    # Determine which update method to call
    if config.within.nil?
      # Class-level index (unique_index without within:)
      send("update_in_class_#{index_name}", old_field_value)
    else
      # Instance-scoped index (unique_index or multi_index with within:) - requires scope context
      next unless scope_context

      # Use config_name for method naming
      scope_class_config = Familia.resolve_class(config.scope_class).config_name
      send("update_in_#{scope_class_config}_#{index_name}", scope_context, old_field_value)
    end
  end
end