Module: Familia::Features::Relationships::Indexing::InstanceMethods

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

Overview

Instance methods for indexed objects

Instance Method Summary collapse

Instance Method Details

#indexed_in?(index_name) ⇒ Boolean

Check if this object is indexed in a specific context

Returns:

  • (Boolean)


336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/familia/features/relationships/indexing.rb', line 336

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

  # For the cleaned-up API, all indexes are class-level
  index_key = "#{self.class.name.downcase}:#{index_name}"
  dbclient.hexists(index_key, field_value.to_s)
end

#indexing_membershipsArray<Hash>

Get all indexes this object appears in

Returns:

  • (Array<Hash>)

    Array of index information



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
# File 'lib/familia/features/relationships/indexing.rb', line 305

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

  memberships = []

  self.class.indexing_relationships.each do |config|
    field = config[:field]
    index_name = config[:index_name]
    context_class = config[:context_class]
    field_value = send(field)

    next unless field_value

    # All indexes are stored at class level
    index_key = "#{self.class.name.downcase}:#{index_name}"
    if dbclient.hexists(index_key, field_value.to_s)
      memberships << {
        context_class: context_class == self.class ? 'class' : config[:context_class_name].downcase,
        index_name: index_name,
        field: field,
        field_value: field_value,
        index_key: index_key,
        type: context_class == self.class ? 'class_indexed_by' : 'indexed_by'
      }
    end
  end

  memberships
end

#remove_from_all_indexesObject

Remove from all indexes



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/familia/features/relationships/indexing.rb', line 283

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

  self.class.indexing_relationships.each do |config|
    index_name = config[:index_name]
    context_class = config[:context_class]

    # Determine which remove method to call
    if context_class == self.class
      # Class-level index (class_indexed_by)
      send("remove_from_class_#{index_name}")
    else
      # Relationship index (indexed_by with parent:)
      context_class_name = config[:context_class_name].downcase
      send("remove_from_#{context_class_name}_#{index_name}", nil)
    end
  end
end

#update_all_indexes(old_values = {}) ⇒ Object

Update all indexes



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/familia/features/relationships/indexing.rb', line 261

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

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

    # Determine which update method to call
    if context_class == self.class
      # Class-level index (class_indexed_by)
      send("update_in_class_#{index_name}", old_field_value)
    else
      # Relationship index (indexed_by with parent:)
      context_class_name = config[:context_class_name].downcase
      send("update_in_#{context_class_name}_#{index_name}", nil, old_field_value)
    end
  end
end