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 target-scoped indexes, this only shows class-level indexes since target-scoped indexes require a specific target instance

Returns:

  • (Array<Hash>)

    Array of index information



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

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
    target_class = config.target_class
    cardinality = config.cardinality
    field_value = send(field)

    next unless field_value

    if target_class == self.class
      # 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 << {
        target_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 target instance
      # This would require scanning all possible target instances
      memberships << {
        target_class: config.target_class_config_name,
        index_name: index_name,
        field: field,
        field_value: field_value,
        index_key: 'target_dependent',
        cardinality: cardinality,
        type: cardinality == :unique ? 'unique_index' : 'multi_index',
        note: 'Requires target instance for verification',
      }
    end
  end

  memberships
end

#indexed_in?(index_name) ⇒ Boolean

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

Returns:

  • (Boolean)


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

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

  target_class = config.target_class

  if target_class == self.class
    # 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
    # Target-scoped index (indexed_by) - cannot verify without target instance
    false
  end
end

#remove_from_all_indexes(parent_context = nil) ⇒ Object

Remove from all indexes for a given parent context For class-level indexes (class_indexed_by), parent_context should be nil For relationship indexes (indexed_by), parent_context should be the parent instance



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/familia/features/relationships/indexing.rb', line 172

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

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

    # Determine which remove method to call
    if target_class == self.class
      # Class-level index (unique_index without within:)
      send("remove_from_class_#{index_name}")
    else
      # Relationship index (unique_index or multi_index with within:) - requires parent context
      next unless parent_context

      # Use config_name for method naming
      target_class_config = Familia.resolve_class(config.target_class).config_name
      send("remove_from_#{target_class_config}_#{index_name}", parent_context)
    end
  end
end

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

Update all indexes for a given parent context For class-level indexes (class_indexed_by), parent_context should be nil For relationship indexes (indexed_by), parent_context should be the parent instance



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/familia/features/relationships/indexing.rb', line 145

def update_all_indexes(old_values = {}, parent_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
    target_class = config.target_class
    old_field_value = old_values[field]

    # Determine which update method to call
    if target_class == self.class
      # Class-level index (unique_index without within:)
      send("update_in_class_#{index_name}", old_field_value)
    else
      # Relationship index (unique_index or multi_index with within:) - requires parent context
      next unless parent_context

      # Use config_name for method naming
      target_class_config = Familia.resolve_class(config.target_class).config_name
      send("update_in_#{target_class_config}_#{index_name}", parent_context, old_field_value)
    end
  end
end