Module: Familia::Features::Relationships::Tracking::InstanceMethods
- Defined in:
- lib/familia/features/relationships/tracking.rb
Overview
Instance methods for tracked objects
Instance Method Summary collapse
-
#add_to_class_tracking_collections ⇒ Object
Add to class-level tracking collections automatically.
-
#calculate_tracking_score(context_class, collection_name) ⇒ Float
Calculate the appropriate score for a tracking relationship.
-
#remove_from_all_tracking_collections ⇒ Object
Remove from all tracking collections (used during destroy).
-
#tracking_collections_membership ⇒ Array<Hash>
Get all collections this object appears in.
-
#update_all_tracking_collections ⇒ Object
Update presence in all tracked collections atomically.
Instance Method Details
#add_to_class_tracking_collections ⇒ Object
Add to class-level tracking collections automatically
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/familia/features/relationships/tracking.rb', line 331 def add_to_class_tracking_collections return unless self.class.respond_to?(:tracking_relationships) self.class.tracking_relationships.each do |config| context_class_name = config[:context_class_name] context_class = config[:context_class] collection_name = config[:collection_name] # Only auto-add to class-level collections (where context_class matches self.class) if context_class_name.downcase == self.class.name.downcase # Call the class method to add this object self.class.send("add_to_#{collection_name}", self) end end end |
#calculate_tracking_score(context_class, collection_name) ⇒ Float
Calculate the appropriate score for a tracking relationship
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/familia/features/relationships/tracking.rb', line 271 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_collections ⇒ Object
Remove from all tracking collections (used during destroy)
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 374 375 376 377 378 379 |
# File 'lib/familia/features/relationships/tracking.rb', line 348 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_membership ⇒ Array<Hash>
Get all collections this object appears in
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/familia/features/relationships/tracking.rb', line 384 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_collections ⇒ Object
Update presence in all tracked collections atomically
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/familia/features/relationships/tracking.rb', line 315 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 |