Module: Familia::Features::Relationships::ModelInstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#cleanup_all_relationships!Object

Deprecated.

This method is poorly implemented and will be removed in v3.0. The participation collection removal logic was repetitive and difficult to debug. A cleaner implementation will be provided in a future version. See pull #115 for details.

Note:

Currently only removes from indexes, not participation collections

Comprehensive cleanup - remove from all relationships



224
225
226
227
228
229
230
# File 'lib/familia/features/relationships.rb', line 224

def cleanup_all_relationships!
  warn '[DEPRECATED] cleanup_all_relationships! will be removed in v3.0. See pull #115.'
  warn 'Not currently removing from participation collections. Only indexes will be cleaned.'

  # Remove from indexes
  remove_from_all_indexes if respond_to?(:remove_from_all_indexes)
end

#create_temp_key(base_name, ttl = 300) ⇒ Object

Instance method wrapper for create_temp_key



258
259
260
261
262
263
264
265
266
267
# File 'lib/familia/features/relationships.rb', line 258

def create_temp_key(base_name, ttl = 300)
  timestamp = Familia.now.to_i
  random_suffix = SecureRandom.hex(3)
  temp_key = Familia.join('temp', base_name, timestamp, random_suffix)

  # UnsortedSet immediate expiry to ensure cleanup even if operation fails
  dbclient.expire(temp_key, ttl)

  temp_key
end

#dbclientObject

Direct Valkey/Redis access for instance methods



253
254
255
# File 'lib/familia/features/relationships.rb', line 253

def dbclient
  self.class.dbclient
end

#relationship_statusObject

Get comprehensive relationship status for this object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/familia/features/relationships.rb', line 200

def relationship_status
  status = {
    identifier: identifier,
    current_participations: [],
    index_memberships: [],
  }

  # Get participation memberships
  status[:current_participations] = current_participations if respond_to?(:current_participations)

  # Get index memberships
  status[:index_memberships] = current_indexings if respond_to?(:current_indexings)

  status
end

#save(update_expiration: true) ⇒ Object

Override save to update relationships automatically



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/familia/features/relationships.rb', line 185

def save(update_expiration: true)
  result = super

  if result && respond_to?(:update_all_indexes)
    # Automatically update all indexes when object is saved
    update_all_indexes

    # NOTE: Relationship-specific participation updates are done explicitly
    # since we need to know which specific collections this object should be in
  end

  result
end

#validate_relationships!Object

Validate that this object's relationships are consistent

Raises:



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/familia/features/relationships.rb', line 233

def validate_relationships!
  errors = []

  # Validate identifier exists
  errors << 'Object identifier is nil' unless identifier

  # Validate participation memberships
  if respond_to?(:current_participations)
    current_participations.each do |membership|
      score = membership[:score]
      errors << "Invalid score in participation membership: #{membership}" if score && !score.is_a?(Numeric)
    end
  end

  raise RelationshipError, "Relationship validation failed for #{self}: #{errors.join('; ')}" if errors.any?

  true
end