Module: Familia::Features::Relationships::CollectionOperations
- Defined in:
- lib/familia/features/relationships/collection_operations.rb
Overview
Shared collection operations for Participation module Provides common methods for working with Horreum-managed DataType collections Used by both ParticipantMethods and TargetMethods to reduce duplication
Instance Method Summary collapse
-
#add_to_collection(collection, item, type:, score: nil, target_class: nil, collection_name: nil) ⇒ Object
Add an item to a collection, handling type-specific operations.
-
#bulk_add_to_collection(collection, items, type:, target_class: nil, collection_name: nil) ⇒ Object
Bulk add items to a collection using DataType methods.
-
#ensure_collection_field(target_class, collection_name, type) ⇒ Object
Ensure a target class has the specified DataType field defined.
-
#member_of_collection?(collection, item) ⇒ Boolean
Check if an item is a member of a collection.
-
#remove_from_collection(collection, item, type: nil) ⇒ Object
Remove an item from a collection.
Instance Method Details
#add_to_collection(collection, item, type:, score: nil, target_class: nil, collection_name: nil) ⇒ Object
Add an item to a collection, handling type-specific operations
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/familia/features/relationships/collection_operations.rb', line 27 def add_to_collection(collection, item, type:, score: nil, target_class: nil, collection_name: nil) case type when :sorted_set # Ensure score is never nil for sorted sets score ||= calculate_item_score(item, target_class, collection_name) collection.add(item.identifier, score) when :list # Lists use push/unshift operations collection.add(item.identifier) when :set # Sets use simple add collection.add(item.identifier) else raise ArgumentError, "Unknown collection type: #{type}" end end |
#bulk_add_to_collection(collection, items, type:, target_class: nil, collection_name: nil) ⇒ Object
Bulk add items to a collection using DataType methods
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/familia/features/relationships/collection_operations.rb', line 65 def bulk_add_to_collection(collection, items, type:, target_class: nil, collection_name: nil) return if items.empty? case type when :sorted_set # Add items one by one for sorted sets to ensure proper scoring items.each do |item| score = calculate_item_score(item, target_class, collection_name) collection.add(item.identifier, score) end when :set, :list # For sets and lists, add items one by one using DataType methods items.each do |item| collection.add(item.identifier) end else raise ArgumentError, "Unknown collection type: #{type}" end end |
#ensure_collection_field(target_class, collection_name, type) ⇒ Object
Ensure a target class has the specified DataType field defined
16 17 18 19 20 |
# File 'lib/familia/features/relationships/collection_operations.rb', line 16 def ensure_collection_field(target_class, collection_name, type) return if target_class.method_defined?(collection_name) target_class.send(type, collection_name) end |
#member_of_collection?(collection, item) ⇒ Boolean
Check if an item is a member of a collection
57 58 59 |
# File 'lib/familia/features/relationships/collection_operations.rb', line 57 def member_of_collection?(collection, item) collection.member?(item.identifier) end |
#remove_from_collection(collection, item, type: nil) ⇒ Object
Remove an item from a collection
48 49 50 51 |
# File 'lib/familia/features/relationships/collection_operations.rb', line 48 def remove_from_collection(collection, item, type: nil) # All collection types support remove/delete collection.remove(item.identifier) end |