Class: Familia::Features::ObjectIdentifiers::ObjectIdentifierFieldType

Inherits:
Familia::FieldType
  • Object
show all
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.

Examples:

Using object identifier fields

class User < Familia::Horreum
  feature :object_identifiers, generator: :uuid_v7
end

user = User.new
user.objid  # Generates UUID v7 on first access

# Loading existing object preserves ID
user2 = User.new(objid: "existing-uuid")
user2.objid  # Returns "existing-uuid", not regenerated

Instance Attribute Summary

Attributes inherited from Familia::FieldType

#fast_method_name, #loggable, #method_name, #name, #on_conflict, #options

Instance Method Summary collapse

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

#categorySymbol

Category for object identifier fields

Returns:

  • (Symbol)

    :object_identifier



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.

Parameters:

  • klass (Class)

    The class to define the method on



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.

Parameters:

  • klass (Class)

    The class to define the method on



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

Returns:

  • (Boolean)

    true - object identifiers are always persisted



77
78
79
# File 'lib/familia/features/object_identifiers/object_identifier_field_type.rb', line 77

def persistent?
  true
end