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?(context_instance, index_name) ⇒ Boolean

Check if this object is indexed in a specific context

Returns:

  • (Boolean)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/familia/features/relationships/indexing.rb', line 346

def indexed_in?(context_instance, 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[:context_class_name] == 'global'
    index_key = "global:#{index_name}"
  else
    context_class_name = config[:context_class_name]
    index_key = "#{context_class_name.downcase}:#{context_instance.identifier}:#{index_name}"
  end

  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



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

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

  memberships = []

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

    next unless field_value

    if context_class_name == 'global'
      index_key = "global:#{index_name}"
      if dbclient.hexists(index_key, field_value.to_s)
        memberships << {
          context_class: 'global',
          index_name: index_name,
          field: field,
          field_value: field_value,
          index_key: index_key
        }
      end
    else
      # Scan for all context instances that have this object indexed
      pattern = "#{context_class_name.downcase}:*:#{index_name}"

      dbclient.scan_each(match: pattern) do |key|
        if dbclient.hexists(key, field_value.to_s)
          context_id = key.split(':')[1]
          memberships << {
            context_class: context_class_name,
            context_id: context_id,
            index_name: index_name,
            field: field,
            field_value: field_value,
            index_key: key
          }
        end
      end
    end
  end

  memberships
end

#remove_from_all_indexesObject

Remove from all indexes (used during destroy)



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/familia/features/relationships/indexing.rb', line 270

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

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

    if context_class_name == 'global'
      send("remove_from_global_#{index_name}")
    else
      # For non-global indexes, we'd need to find all context instances
      # that have this object indexed. This is expensive but necessary for cleanup.
      pattern = "#{context_class_name.downcase}:*:#{index_name}"
      field_value = send(field)

      next unless field_value

      dbclient.scan_each(match: pattern) do |key|
        dbclient.hdel(key, field_value.to_s)
      end
    end
  end
end

#update_all_indexes(old_values = {}) ⇒ Object

Update all indexes that this object participates in



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

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

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

    old_field_value = old_values[field]

    if context_class_name == 'global'
      send("update_in_global_#{index_name}", old_field_value)
    else
      # For non-global indexes, we'd need to know which context instances
      # this object should be indexed in. This is a simplified approach.
      # In practice, you'd need to track relationships or pass context.
    end
  end
end