Module: Familia::Features::Relationships::Indexing::UniqueIndexGenerators
- Defined in:
- lib/familia/features/relationships/indexing/unique_index_generators.rb
Overview
Generators for unique index (1:1) methods
Unique indexes use HashKey DataType for field-to-object identifier mapping. Each field value maps to exactly one object identifier.
Example (instance-scoped): unique_index :badge_number, :badge_index, within: Company
Generates on Company (destination):
- company.find_by_badge_number(badge)
- company.find_all_by_badge_number([badges])
- company.badge_index
- company.rebuild_badge_index
Generates on Employee (self):
- employee.add_to_company_badge_index(company)
- employee.remove_from_company_badge_index(company)
- employee.update_in_company_badge_index(company, old_badge)
Example (class-level): unique_index :email, :email_index
Generates on Employee (class):
- Employee.find_by_email(email)
- Employee.find_all_by_email([emails])
- Employee.email_index
- Employee.rebuild_email_index
Generates on Employee (self):
- employee.add_to_class_email_index (called automatically on save)
- employee.remove_from_class_email_index
- employee.update_in_class_email_index(old_email)
Note: Class-level indexes auto-populate on save(). Instance-scoped indexes (with within:) remain manual as they require parent context.
Class Method Summary collapse
-
.generate_mutation_methods_class(field, index_name, indexed_class) ⇒ Object
Generates mutation methods ON THE INDEXED CLASS (Employee): Instance methods for class-level index operations: - employee.add_to_class_email_index - employee.remove_from_class_email_index - employee.update_in_class_email_index(old_email).
-
.generate_mutation_methods_self(indexed_class, field, target_class, index_name) ⇒ Object
Generates mutation methods ON THE INDEXED CLASS (Employee): Instance methods for parent-scoped unique index operations: - employee.add_to_company_badge_index(company) - employee.remove_from_company_badge_index(company) - employee.update_in_company_badge_index(company, old_badge).
-
.generate_query_methods_class(field, index_name, indexed_class) ⇒ Object
Generates query methods ON THE INDEXED CLASS (Employee): Class-level methods (singleton): - Employee.find_by_email(email) - Employee.find_all_by_email([emails]) - Employee.email_index - Employee.rebuild_email_index.
-
.generate_query_methods_destination(indexed_class, field, target_class, index_name) ⇒ Object
Generates query methods ON THE PARENT CLASS (Company when within: Company): - company.find_by_badge_number(badge) - find by field value - company.find_all_by_badge_number([badges]) - batch lookup - company.badge_index - DataType accessor - company.rebuild_badge_index - rebuild index.
-
.setup(indexed_class:, field:, index_name:, within:, query:) ⇒ Object
Main setup method that orchestrates unique index creation.
Class Method Details
.generate_mutation_methods_class(field, index_name, indexed_class) ⇒ Object
Generates mutation methods ON THE INDEXED CLASS (Employee): Instance methods for class-level index operations:
- employee.add_to_class_email_index
- employee.remove_from_class_email_index
- employee.update_in_class_email_index(old_email)
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 313 314 315 |
# File 'lib/familia/features/relationships/indexing/unique_index_generators.rb', line 280 def generate_mutation_methods_class(field, index_name, indexed_class) indexed_class.class_eval do define_method(:"add_to_class_#{index_name}") do index_hash = self.class.send(index_name) # Access the class-level hashkey DataType field_value = send(field) return unless field_value index_hash[field_value.to_s] = identifier end define_method(:"remove_from_class_#{index_name}") do index_hash = self.class.send(index_name) # Access the class-level hashkey DataType field_value = send(field) return unless field_value index_hash.remove(field_value.to_s) end define_method(:"update_in_class_#{index_name}") do |old_field_value = nil| new_field_value = send(field) # Use class-level transaction for atomicity with DataType abstraction self.class.transaction do |_tx| index_hash = self.class.send(index_name) # Access the class-level hashkey DataType # Remove old value if provided index_hash.remove(old_field_value.to_s) if old_field_value # Add new value if present index_hash[new_field_value.to_s] = identifier if new_field_value end end end end |
.generate_mutation_methods_self(indexed_class, field, target_class, index_name) ⇒ Object
Generates mutation methods ON THE INDEXED CLASS (Employee): Instance methods for parent-scoped unique index operations:
- employee.add_to_company_badge_index(company)
- employee.remove_from_company_badge_index(company)
- employee.update_in_company_badge_index(company, old_badge)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/familia/features/relationships/indexing/unique_index_generators.rb', line 166 def generate_mutation_methods_self(indexed_class, field, target_class, index_name) target_class_config = target_class.config_name indexed_class.class_eval do method_name = :"add_to_#{target_class_config}_#{index_name}" Familia.ld("[UniqueIndexGenerators] #{name} method #{method_name}") define_method(method_name) do |target_instance| return unless target_instance field_value = send(field) return unless field_value # Use declared field accessor on target instance index_hash = target_instance.send(index_name) # Use HashKey DataType method index_hash[field_value.to_s] = identifier end method_name = :"remove_from_#{target_class_config}_#{index_name}" Familia.ld("[UniqueIndexGenerators] #{name} method #{method_name}") define_method(method_name) do |target_instance| return unless target_instance field_value = send(field) return unless field_value # Use declared field accessor on target instance index_hash = target_instance.send(index_name) # Remove using HashKey DataType method index_hash.remove(field_value.to_s) end method_name = :"update_in_#{target_class_config}_#{index_name}" Familia.ld("[UniqueIndexGenerators] #{name} method #{method_name}") define_method(method_name) do |target_instance, old_field_value = nil| return unless target_instance new_field_value = send(field) # Use Familia's transaction method for atomicity with DataType abstraction target_instance.transaction do |_tx| # Use declared field accessor on target instance index_hash = target_instance.send(index_name) # Remove old value if provided index_hash.remove(old_field_value.to_s) if old_field_value # Add new value if present index_hash[new_field_value.to_s] = identifier if new_field_value end end end end |
.generate_query_methods_class(field, index_name, indexed_class) ⇒ Object
Generates query methods ON THE INDEXED CLASS (Employee): Class-level methods (singleton):
- Employee.find_by_email(email)
- Employee.find_all_by_email([emails])
- Employee.email_index
- Employee.rebuild_email_index
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 |
# File 'lib/familia/features/relationships/indexing/unique_index_generators.rb', line 230 def generate_query_methods_class(field, index_name, indexed_class) indexed_class.define_singleton_method(:"find_by_#{field}") do |provided_id| index_hash = send(index_name) # Access the class-level hashkey DataType # Get the identifier from the hash using .get method. # We use .get instead of [] because it's part of the standard interface # common across all DataType classes (List, UnsortedSet, SortedSet, HashKey). # While unique indexes always use HashKey, using .get maintains consistency # with the broader DataType API patterns used throughout Familia. record_id = index_hash.get(provided_id) return nil unless record_id indexed_class.find_by_identifier(record_id) end # Generate class-level bulk query method indexed_class.define_singleton_method(:"find_all_by_#{field}") do |provided_ids| provided_ids = Array(provided_ids) return [] if provided_ids.empty? index_hash = send(index_name) # Access the class-level hashkey DataType record_ids = index_hash.values_at(*provided_ids.map(&:to_s)) # Filter out nil values and instantiate objects record_ids.compact.map { |record_id| indexed_class.find_by_identifier(record_id) } end # The index accessor method is already created by the class_hashkey declaration # No need to manually create it - Horreum handles this automatically # Generate method to rebuild the class-level index indexed_class.define_singleton_method(:"rebuild_#{index_name}") do index_hash = send(index_name) # Access the class-level hashkey DataType # Clear existing index using DataType method index_hash.clear # Rebuild from all existing objects # This would need to scan through all objects of this class # Implementation depends on how objects are stored/tracked end end |
.generate_query_methods_destination(indexed_class, field, target_class, index_name) ⇒ Object
Generates query methods ON THE PARENT CLASS (Company when within: Company):
- company.find_by_badge_number(badge) - find by field value
- company.find_all_by_badge_number([badges]) - batch lookup
- company.badge_index - DataType accessor
- company.rebuild_badge_index - rebuild index
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/familia/features/relationships/indexing/unique_index_generators.rb', line 98 def generate_query_methods_destination(indexed_class, field, target_class, index_name) # Resolve target class using Familia pattern actual_target_class = Familia.resolve_class(target_class) # Ensure the index field is declared (creates accessor that returns DataType) actual_target_class.send(:ensure_index_field, actual_target_class, index_name, :hashkey) # Generate instance query method (e.g., company.find_by_badge_number) actual_target_class.class_eval do define_method(:"find_by_#{field}") do |provided_value| # Use declared field accessor instead of manual instantiation index_hash = send(index_name) # Get the identifier from the hash using .get method. # We use .get instead of [] because it's part of the standard interface # common across all DataType classes (List, UnsortedSet, SortedSet, HashKey). # While unique indexes always use HashKey, using .get maintains consistency # with the broader DataType API patterns used throughout Familia. record_id = index_hash.get(provided_value) return nil unless record_id indexed_class.find_by_identifier(record_id) end # Generate bulk query method (e.g., company.find_all_by_badge_number) define_method(:"find_all_by_#{field}") do |provided_ids| provided_ids = Array(provided_ids) return [] if provided_ids.empty? # Use declared field accessor instead of manual instantiation index_hash = send(index_name) # Get all identifiers from the hash record_ids = index_hash.values_at(*provided_ids.map(&:to_s)) # Filter out nil values and instantiate objects record_ids.compact.map { |record_id| indexed_class.find_by_identifier(record_id) } end # Accessor method already created by ensure_index_field above # No need to manually define it here # Generate method to rebuild the unique index for this parent instance define_method(:"rebuild_#{index_name}") do # Use declared field accessor instead of manual instantiation index_hash = send(index_name) # Clear existing index using DataType method index_hash.clear # Rebuild from all existing objects # This would need to scan through all objects belonging to this parent # Implementation depends on how objects are stored/tracked end end end |
.setup(indexed_class:, field:, index_name:, within:, query:) ⇒ Object
Main setup method that orchestrates unique index creation
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/familia/features/relationships/indexing/unique_index_generators.rb', line 54 def setup(indexed_class:, field:, index_name:, within:, query:) # Normalize parameters and determine scope type target_class, scope_type = if within k = Familia.resolve_class(within) [k, :instance] else [indexed_class, :class] end # Store metadata for this indexing relationship indexed_class.indexing_relationships << IndexingRelationship.new( field: field, target_class: target_class, index_name: index_name, query: query, cardinality: :unique, ) # Generate appropriate methods based on scope type case scope_type when :instance # Instance-scoped index (within: Company) if query && target_class.is_a?(Class) generate_query_methods_destination(indexed_class, field, target_class, index_name) end generate_mutation_methods_self(indexed_class, field, target_class, index_name) when :class # Class-level index (no within:) indexed_class.send(:ensure_index_field, indexed_class, index_name, :class_hashkey) generate_query_methods_class(field, index_name, indexed_class) if query generate_mutation_methods_class(field, index_name, indexed_class) end end |