Module: Familia::Horreum::DefinitionMethods
- Includes:
- RelatedFieldsManagement, Settings
- Defined in:
- lib/familia/horreum/definition.rb
Overview
DefinitionMethods - Class-level DSL methods for defining Horreum model structure
This module is extended into classes that include Familia::Horreum, providing class methods for defining model structure and configuration (e.g., Customer.field :name, Customer.identifier_field :custid).
Key features:
- Defines DSL methods for field definitions (field, identifier_field)
- Includes RelatedFieldsManagement for DataType field DSL (list, set, zset, etc.)
- Provides class-level configuration (prefix, suffix, logical_database)
- Manages field metadata and inheritance
Instance Attribute Summary
Attributes included from Settings
#current_key_version, #default_expiration, #delim, #encryption_keys, #encryption_personalization, #raise_on_unsaved_parent_write, #schema_path, #schema_validator, #schemas, #strict_write_order, #transaction_mode
Instance Method Summary collapse
-
#add_feature_options(feature_name, **options) ⇒ Hash
Add feature options for a specific feature.
- #class_related_fields ⇒ Object
-
#dirty_write_warnings(mode = nil) ⇒ Symbol
Sets or retrieves how collection writes on this class's DataTypes react when the parent instance has unsaved scalar field changes.
- #dirty_write_warnings=(mode) ⇒ Object
-
#feature_options(feature_name = nil) ⇒ Hash
Retrieves feature options for the current class.
-
#field(name, as: name, fast_method: :"#{name}!", on_conflict: :raise) ⇒ Object
Defines a field for the class and creates accessor methods.
-
#field_group(name) { ... } ⇒ Array<Symbol>
Defines a field group to organize related fields.
-
#field_groups ⇒ Array<Symbol>
Returns the list of all field group names defined for the class.
-
#field_method_map ⇒ Object
Returns a hash mapping field names to method names for backward compatibility.
-
#field_types ⇒ Object
Storage for field type instances.
-
#fields ⇒ Object
Returns the list of field names defined for the class in the order that they were defined.
-
#identifier_field(val = nil) ⇒ Object
Sets or retrieves the unique identifier field for the class.
- #logical_database(num = nil) ⇒ Object
-
#persistent_fields ⇒ Object
Get fields for serialization (excludes transients).
-
#prefix(val = nil) ⇒ String, Symbol
Sets or retrieves the prefix for generating Valkey/Redis keys.
-
#register_field_type(field_type) ⇒ Object
Register a field type instance with this class.
- #related_fields ⇒ Object
- #relations? ⇒ Boolean
-
#suffix(val = nil, &blk) ⇒ String, Symbol
Sets or retrieves the suffix for generating Valkey/Redis keys.
-
#transient_fields ⇒ Object
Get fields that are not persisted to the database (transients).
Methods included from RelatedFieldsManagement
#attach_class_related_field, #attach_instance_related_field
Methods included from Settings
#configure, #default_suffix, #pipelined_mode, #pipelined_mode=
Instance Method Details
#add_feature_options(feature_name, **options) ⇒ Hash
This method only sets defaults for options that don't already exist, using the ||= operator to prevent overwrites.
Add feature options for a specific feature
This method provides a clean way for features to set their default options without worrying about initialization state. Similar to register_field_type for field types.
Feature options are stored at the class level using instance variables, ensuring complete isolation between different Familia::Horreum subclasses. Each class maintains its own @feature_options hash.
411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/familia/horreum/definition.rb', line 411 def (feature_name, **) @feature_options ||= {} @feature_options[feature_name.to_sym] ||= {} # Only set defaults for options that don't already exist .each do |key, value| @feature_options[feature_name.to_sym][key] ||= value end @feature_options[feature_name.to_sym] end |
#class_related_fields ⇒ Object
280 281 282 283 |
# File 'lib/familia/horreum/definition.rb', line 280 def @class_related_fields ||= {} @class_related_fields end |
#dirty_write_warnings(mode = nil) ⇒ Symbol
Sets or retrieves how collection writes on this class's DataTypes react when the parent instance has unsaved scalar field changes.
Mirrors +Familia.strict_write_order+ but is scoped to a single Horreum subclass, so seed scripts, bulk importers, and known-safe call sites can opt down without touching the global setting.
Resolution order (highest precedence first):
- Active +atomic_write+ block -- suppresses everything.
- Class-level +:off+ -- suppresses both warnings AND raises, overriding +strict_write_order+ and +raise_on_unsaved_parent_write+. "Off means off."
- Raise gates: +Familia.strict_write_order = true+, class +:strict+, or a new/unsaved parent when +raise_on_unsaved_parent_write+ is true.
- Otherwise warn per the resolved mode: this class-level setting (inherited through the subclass chain), else +Familia.dirty_write_warnings+ (:once when unset).
- :strict - raise Familia::Problem (overrides global strict_write_order=false)
- :warn - warn on every collection write (legacy behavior)
- :once - warn once per distinct dirty-field signature per window [default]
- :off - suppress entirely for this class
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/familia/horreum/definition.rb', line 253 def dirty_write_warnings(mode = nil) unless mode.nil? valid = %i[strict warn once off] unless valid.include?(mode) raise ArgumentError, "dirty_write_warnings must be one of #{valid.inspect}, got #{mode.inspect}" end return @dirty_write_warnings = mode end @dirty_write_warnings || (superclass.respond_to?(:dirty_write_warnings) ? superclass.dirty_write_warnings : nil) || Familia.dirty_write_warnings end |
#dirty_write_warnings=(mode) ⇒ Object
267 268 269 |
# File 'lib/familia/horreum/definition.rb', line 267 def dirty_write_warnings=(mode) dirty_write_warnings(mode) end |
#feature_options(feature_name = nil) ⇒ Hash
Retrieves feature options for the current class.
Feature options are stored per-class in instance variables, ensuring complete isolation between different Familia::Horreum subclasses. Each class maintains its own @feature_options hash that does not interfere with other classes' configurations.
376 377 378 379 380 381 |
# File 'lib/familia/horreum/definition.rb', line 376 def (feature_name = nil) @feature_options ||= {} return @feature_options if feature_name.nil? @feature_options[feature_name.to_sym] || {} end |
#field(name, as: name, fast_method: :"#{name}!", on_conflict: :raise) ⇒ Object
Defines a field for the class and creates accessor methods.
This method defines a new field for the class, creating getter and setter
instance methods similar to attr_accessor. It also generates a fast
writer method for immediate persistence to the database.
181 182 183 184 |
# File 'lib/familia/horreum/definition.rb', line 181 def field(name, as: name, fast_method: :"#{name}!", on_conflict: :raise) field_type = FieldType.new(name, as: as, fast_method: fast_method, on_conflict: on_conflict) register_field_type(field_type) end |
#field_group(name) { ... } ⇒ Array<Symbol>
Defines a field group to organize related fields.
Field groups provide a way to categorize and query fields by purpose or feature. When a block is provided, fields defined within the block are automatically added to the group. Without a block, an empty group is initialized.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/familia/horreum/definition.rb', line 86 def field_group(name, &block) # Prevent nested field groups if @current_field_group raise Familia::Problem, "Cannot define field group :#{name} while :#{@current_field_group} is being defined. " \ "Nested field groups are not supported." end # Initialize group field_groups[name.to_sym] ||= [] if block_given? @current_field_group = name.to_sym begin instance_eval(&block) ensure @current_field_group = nil end else Familia.debug "[field_group] Created field group :#{name} but no block given" end field_groups[name.to_sym] end |
#field_groups ⇒ Array<Symbol>
Returns the list of all field group names defined for the class.
131 132 133 134 135 136 |
# File 'lib/familia/horreum/definition.rb', line 131 def field_groups @field_groups_mutex ||= Familia::ThreadSafety::InstrumentedMutex.new('field_groups') @field_groups || @field_groups_mutex.synchronize do @field_groups ||= {} end end |
#field_method_map ⇒ Object
Returns a hash mapping field names to method names for backward compatibility
303 304 305 |
# File 'lib/familia/horreum/definition.rb', line 303 def field_method_map field_types.transform_values(&:method_name) end |
#field_types ⇒ Object
Storage for field type instances
295 296 297 298 299 300 |
# File 'lib/familia/horreum/definition.rb', line 295 def field_types @field_types_mutex ||= Familia::ThreadSafety::InstrumentedMutex.new('field_types') @field_types || @field_types_mutex.synchronize do @field_types ||= {} end end |
#fields ⇒ Object
Returns the list of field names defined for the class in the order
that they were defined. i.e. field :a; field :b; fields => [:a, :b].
273 274 275 276 277 278 |
# File 'lib/familia/horreum/definition.rb', line 273 def fields @fields_mutex ||= Familia::ThreadSafety::InstrumentedMutex.new('fields') @fields || @fields_mutex.synchronize do @fields ||= [] end end |
#identifier_field(val = nil) ⇒ Object
Sets or retrieves the unique identifier field for the class.
This method defines or returns the field or method that contains the unique identifier used to generate the dbkey for the object. If a value is provided, it sets the identifier field; otherwise, it returns the current identifier field.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/familia/horreum/definition.rb', line 147 def identifier_field(val = nil) if val # Validate identifier field definition at class definition time case val when Symbol, String, Proc @identifier_field = val else raise Problem, <<~ERROR Invalid identifier field definition: #{val.inspect}. Use a field name (Symbol/String) or Proc. ERROR end end @identifier_field end |
#logical_database(num = nil) ⇒ Object
217 218 219 220 221 |
# File 'lib/familia/horreum/definition.rb', line 217 def logical_database(num = nil) Familia.trace :LOGICAL_DATABASE_DEF, "instvar:#{@logical_database}", num if Familia.debug? @logical_database = num unless num.nil? @logical_database || parent&.logical_database end |
#persistent_fields ⇒ Object
Get fields for serialization (excludes transients)
308 309 310 311 312 |
# File 'lib/familia/horreum/definition.rb', line 308 def persistent_fields fields.select do |field| field_types[field]&.persistent? end end |
#prefix(val = nil) ⇒ String, Symbol
Sets or retrieves the prefix for generating Valkey/Redis keys.
The exception is only raised when both @prefix is nil/falsy AND name is nil, which typically occurs with anonymous classes that haven't had their prefix explicitly set.
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/familia/horreum/definition.rb', line 206 def prefix(val = nil) @prefix = val if val @prefix || begin if name.nil? raise Problem, 'Cannot generate prefix for anonymous class. ' \ 'Use `prefix` method to set explicitly.' end config_name.to_sym end end |
#register_field_type(field_type) ⇒ Object
Register a field type instance with this class
This method installs the field type's methods and registers it for later reference. It maintains backward compatibility by creating FieldDefinition objects.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/familia/horreum/definition.rb', line 329 def register_field_type(field_type) fields << field_type.name field_type.install(self) # Complete the registration after installation. If we do this beforehand # we can run into issues where it looks like it's already installed. field_types[field_type.name] = field_type # Add to current field group if one is active if @current_field_group @field_groups[@current_field_group] << field_type.name end # Freeze the field_type to ensure immutability (maintains Data class heritage) field_type.freeze end |
#related_fields ⇒ Object
285 286 287 288 |
# File 'lib/familia/horreum/definition.rb', line 285 def @related_fields ||= {} @related_fields end |
#relations? ⇒ Boolean
290 291 292 |
# File 'lib/familia/horreum/definition.rb', line 290 def relations? @has_related_fields ||= false end |
#suffix(val = nil, &blk) ⇒ String, Symbol
Sets or retrieves the suffix for generating Valkey/Redis keys.
192 193 194 195 |
# File 'lib/familia/horreum/definition.rb', line 192 def suffix(val = nil, &blk) @suffix = val || blk if val || !blk.nil? @suffix || Familia.default_suffix end |
#transient_fields ⇒ Object
Get fields that are not persisted to the database (transients)
315 316 317 318 319 |
# File 'lib/familia/horreum/definition.rb', line 315 def transient_fields fields.select do |field| field_types[field]&.transient? end end |