Module: Familia::Features::Relationships::Indexing::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#indexed_by(field, index_name, context:, finder: true) ⇒ Object

Define an indexed_by relationship for fast lookups

Examples:

Basic indexing

indexed_by :display_name, context: Customer, index_name: :domain_index

Global indexing

indexed_by :domain_id, context: :global, index_name: :domain_lookup

Parameters:

  • field (Symbol)

    The field to index on

  • context (Class, Symbol)

    The context class that owns the index

  • index_name (Symbol)

    Name of the index hash

  • finder (Boolean) (defaults to: true)

    Whether to generate finder methods



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/familia/features/relationships/indexing.rb', line 29

def indexed_by(field, index_name, context:, finder: true)
  context_class = context == :global ? :global : context
  context_class_name = if context_class == :global
                         'global'
                       else
                         (context_class.is_a?(Class) ? context_class.name : context_class.to_s.camelize)
                       end

  # Store metadata for this indexing relationship
  indexing_relationships << {
    field: field,
    context_class: context_class,
    context_class_name: context_class_name,
    index_name: index_name,
    finder: finder
  }

  # Generate finder methods on the context class
  if finder && context_class != :global
    generate_context_finder_methods(context_class, field, index_name)
  elsif finder && context_class == :global
    generate_global_finder_methods(field, index_name)
  end

  # Generate instance methods for maintaining the index
  generate_indexing_instance_methods(context_class_name, field, index_name)
end

#indexing_relationshipsObject

Get all indexing relationships for this class



58
59
60
# File 'lib/familia/features/relationships/indexing.rb', line 58

def indexing_relationships
  @indexing_relationships ||= []
end