Class: Familia::Features::ObjectIdentifiers::ObjectIdentifierFieldType
- Inherits:
-
Familia::FieldType
- Object
- Familia::FieldType
- Familia::Features::ObjectIdentifiers::ObjectIdentifierFieldType
- Defined in:
- lib/familia/features/object_identifiers/object_identifier_field_type.rb
Overview
ObjectIdentifierFieldType - Fields that generate unique object identifiers
Object identifier fields automatically generate unique identifiers when first accessed if not already set. The generation strategy is configurable via feature options. These fields preserve any values set during initialization to ensure data integrity when loading existing objects from Redis.
Instance Attribute Summary
Attributes inherited from Familia::FieldType
#fast_method_name, #loggable, #method_name, #name, #on_conflict, #options
Instance Method Summary collapse
-
#category ⇒ Symbol
Category for object identifier fields.
-
#define_getter(klass) ⇒ Object
Override getter to provide lazy generation with configured strategy.
-
#define_setter(klass) ⇒ Object
Override setter to preserve values during initialization.
-
#persistent? ⇒ Boolean
Object identifier fields are persisted to database.
Methods inherited from Familia::FieldType
#define_fast_writer, #deserialize, #generated_methods, #initialize, #inspect, #install, #serialize, #transient?
Constructor Details
This class inherits a constructor from Familia::FieldType
Instance Method Details
#category ⇒ Symbol
Category for object identifier fields
85 86 87 |
# File 'lib/familia/features/object_identifiers/object_identifier_field_type.rb', line 85 def category :object_identifier end |
#define_getter(klass) ⇒ Object
Override getter to provide lazy generation with configured strategy
Generates the identifier using the configured strategy if not already set. This preserves any values set during initialization while providing automatic generation for new objects.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/familia/features/object_identifiers/object_identifier_field_type.rb', line 36 def define_getter(klass) field_name = @name method_name = @method_name handle_method_conflict(klass, method_name) do klass.define_method method_name do # Check if we already have a value (from initialization or previous generation) existing_value = instance_variable_get(:"@#{field_name}") return existing_value unless existing_value.nil? # Generate new identifier using configured strategy generated_id = generate_object_identifier instance_variable_set(:"@#{field_name}", generated_id) generated_id end end end |
#define_setter(klass) ⇒ Object
Override setter to preserve values during initialization
This ensures that values passed during object initialization (e.g., when loading from Redis) are preserved and not overwritten by the lazy generation logic.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/familia/features/object_identifiers/object_identifier_field_type.rb', line 62 def define_setter(klass) field_name = @name method_name = @method_name handle_method_conflict(klass, :"#{method_name}=") do klass.define_method :"#{method_name}=" do |value| instance_variable_set(:"@#{field_name}", value) end end end |
#persistent? ⇒ Boolean
Object identifier fields are persisted to database
77 78 79 |
# File 'lib/familia/features/object_identifiers/object_identifier_field_type.rb', line 77 def persistent? true end |