Module: Familia::Features::Relationships::Tracking::InstanceMethods

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

Overview

Instance methods for tracked objects

Instance Method Summary collapse

Instance Method Details

#calculate_tracking_score(context_class, collection_name) ⇒ Float

Calculate the appropriate score for a tracking relationship

Parameters:

  • context_class (Class)

    The context class (e.g., Customer)

  • collection_name (Symbol)

    The collection name (e.g., :domains)

Returns:

  • (Float)

    Calculated score



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/familia/features/relationships/tracking.rb', line 248

def calculate_tracking_score(context_class, collection_name)
  # Find the tracking configuration
  tracking_config = self.class.tracking_relationships.find do |config|
    config[:context_class] == context_class && config[:collection_name] == collection_name
  end

  return current_score unless tracking_config

  score_calculator = tracking_config[:score]

  case score_calculator
  when Symbol
    # Field name or method name
    if respond_to?(score_calculator)
      value = send(score_calculator)
      if value.respond_to?(:to_f)
        value.to_f
      elsif value.respond_to?(:to_i)
        encode_score(value, 0)
      else
        current_score
      end
    else
      current_score
    end
  when Proc
    # Execute proc in context of this instance
    result = instance_exec(&score_calculator)
    # Ensure we get a numeric result
    if result.nil?
      current_score
    elsif result.respond_to?(:to_f)
      result.to_f
    else
      current_score
    end
  when Numeric
    score_calculator.to_f
  else
    current_score
  end
end

#remove_from_all_tracking_collectionsObject

Remove from all tracking collections (used during destroy)



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
# File 'lib/familia/features/relationships/tracking.rb', line 308

def remove_from_all_tracking_collections
  return unless self.class.respond_to?(:tracking_relationships)

  # Get all possible collection keys this object might be in
  # This is expensive but necessary for cleanup
  redis_conn = redis
  pattern = '*:*:*' # This could be optimized with better key patterns

  cursor = 0
  matching_keys = []

  loop do
    cursor, keys = redis_conn.scan(cursor, match: pattern, count: 1000)
    matching_keys.concat(keys)
    break if cursor.zero?
  end

  # Filter keys that might contain this object and remove it
  redis_conn.pipelined do |pipeline|
    matching_keys.each do |key|
      # Check if this key matches any of our tracking relationships
      self.class.tracking_relationships.each do |config|
        context_class_name = config[:context_class_name].downcase
        collection_name = config[:collection_name]

        if key.include?(context_class_name) && key.include?(collection_name.to_s)
          pipeline.zrem(key, identifier)
        end
      end
    end
  end
end

#tracking_collections_membershipArray<Hash>

Get all collections this object appears in

Returns:

  • (Array<Hash>)

    Array of collection information



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/familia/features/relationships/tracking.rb', line 344

def tracking_collections_membership
  return [] unless self.class.respond_to?(:tracking_relationships)

  memberships = []

  self.class.tracking_relationships.each do |config|
    context_class_name = config[:context_class_name]
    collection_name = config[:collection_name]

    # Find all instances of context_class where this object appears
    # This is simplified - in practice you'd need a more efficient approach
    pattern = "#{context_class_name.downcase}:*:#{collection_name}"

    dbclient.scan_each(match: pattern) do |key|
      score = dbclient.zscore(key, identifier)
      if score
        context_id = key.split(':')[1]
        memberships << {
          context_class: context_class_name,
          context_id: context_id,
          collection_name: collection_name,
          score: score,
          decoded_score: decode_score(score)
        }
      end
    end
  end

  memberships
end

#update_all_tracking_collectionsObject

Update presence in all tracked collections atomically



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/familia/features/relationships/tracking.rb', line 292

def update_all_tracking_collections
  return unless self.class.respond_to?(:tracking_relationships)

  []

  self.class.tracking_relationships.each do |config|
    config[:context_class_name]
    config[:collection_name]

    # This is a simplified version - in practice, you'd need to know
    # which specific instances this object should be tracked in
    # For now, we'll skip the automatic update and rely on explicit calls
  end
end