Module: Familia::Features::Relationships::Indexing::ModelClassMethods

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

Overview

Indexing::ModelClassMethods

Instance Method Summary collapse

Instance Method Details

#ensure_index_field(scope_class, index_name, field_type) ⇒ Object

Ensure proper DataType field is declared for index Similar to ensure_collection_field in participation system



142
143
144
145
146
# File 'lib/familia/features/relationships/indexing.rb', line 142

def ensure_index_field(scope_class, index_name, field_type)
  return if scope_class.method_defined?(index_name) || scope_class.respond_to?(index_name)

  scope_class.send(field_type, index_name)
end

#indexing_relationshipsObject

Get all indexing relationships for this class



136
137
138
# File 'lib/familia/features/relationships/indexing.rb', line 136

def indexing_relationships
  @indexing_relationships ||= []
end

#multi_index(field, index_name, within: :class, query: true) ⇒ Object

Define an indexed_by relationship for fast lookups

Define a multi-value index (1:many mapping)

Examples:

Instance-scoped multi-value indexing

multi_index :department, :dept_index, within: Company

Parameters:

  • field (Symbol)

    The field to index on

  • index_name (Symbol)

    Name of the index

  • within (Class, Symbol) (defaults to: :class)

    The scope class providing uniqueness context

  • query (Boolean) (defaults to: true)

    Whether to generate query methods



101
102
103
104
105
106
107
108
109
# File 'lib/familia/features/relationships/indexing.rb', line 101

def multi_index(field, index_name, within: :class, query: true)
  MultiIndexGenerators.setup(
    indexed_class: self,
    field: field,
    index_name: index_name,
    within: within,
    query: query,
  )
end

#unique_index(field, index_name, within: nil, query: true) ⇒ Object

Define a unique index lookup (1:1 mapping)

Examples:

Class-level unique index

unique_index :email, :email_lookup
unique_index :username, :username_lookup, query: false

Instance-scoped unique index

unique_index :badge_number, :badge_index, within: Company

Parameters:

  • field (Symbol)

    The field to index on

  • index_name (Symbol)

    Name of the index hash

  • within (Class, Symbol) (defaults to: nil)

    Optional scope class for instance-scoped unique index

  • query (Boolean) (defaults to: true)

    Whether to generate query methods



125
126
127
128
129
130
131
132
133
# File 'lib/familia/features/relationships/indexing.rb', line 125

def unique_index(field, index_name, within: nil, query: true)
  UniqueIndexGenerators.setup(
    indexed_class: self,
    field: field,
    index_name: index_name,
    within: within,
    query: query,
  )
end