Class: Familia::TransientFieldType

Inherits:
FieldType
  • Object
show all
Defined in:
lib/familia/features/transient_fields/transient_field_type.rb

Overview

TransientFieldType - Fields that are not persisted to database

Transient fields automatically wrap values in RedactedString for security and are excluded from serialization operations. They are ideal for storing sensitive data like API keys, passwords, and tokens that should not be persisted to the database.

Examples:

Using transient fields

class SecretService < Familia::Horreum
  field :name                    # Regular field
  transient_field :api_key       # Wrapped in RedactedString
  transient_field :password      # Not persisted to database
end

service = SecretService.new
service.api_key = "sk-1234567890"
service.api_key.class           #=> RedactedString
service.to_h                    #=> {:name => nil} (no api_key)

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Familia::FieldType

Instance Method Details

#categorySymbol

Category for transient fields

Returns:

  • (Symbol)

    :transient



107
108
109
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 107

def category
  :transient
end

#define_fast_writer(_klass) ⇒ Object

Override fast writer to disable it for transient fields

Transient fields should not have fast writers since they're not persisted to the database.

Parameters:

  • _klass (Class)

    The class to define the method on



83
84
85
86
87
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 83

def define_fast_writer(_klass)
  # No fast writer for transient fields since they're not persisted
  Familia.debug "[TransientFieldType] Skipping fast writer for transient field: #{@name}"
  nil
end

#define_getter(klass) ⇒ Object

Override getter to unwrap RedactedString values

Returns the actual value from the RedactedString wrapper for convenient access, or nil if the value is nil or cleared.

Parameters:

  • klass (Class)

    The class to define the method on



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 62

def define_getter(klass)
  field_name = @name
  method_name = @method_name

  handle_method_conflict(klass, method_name) do
    klass.define_method method_name do
      wrapped = instance_variable_get(:"@#{field_name}")
      return nil if wrapped.nil? || wrapped.cleared?

      wrapped
    end
  end
end

#define_setter(klass) ⇒ Object

Override setter to wrap values in RedactedString

Values are automatically wrapped in RedactedString objects for security. Nil values and existing RedactedString objects are handled appropriately.

Parameters:

  • klass (Class)

    The class to define the method on



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 37

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|
      wrapped = if value.nil?
                  nil
                elsif value.is_a?(RedactedString)
                  value
                else
                  RedactedString.new(value)
                end
      instance_variable_set(:"@#{field_name}", wrapped)
    end
  end
end

#deserialize(_value, _record = nil) ⇒ nil

Transient fields are not deserialized from database

This method should not be called since transient fields are not persisted, but we provide it for completeness.

Parameters:

  • _value (Object)

    The value to deserialize

  • _record (Object) (defaults to: nil)

    The record instance

Returns:

  • (nil)

    Always nil since transient fields are not stored



135
136
137
138
139
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 135

def deserialize(_value, _record = nil)
  # Transient fields should never be deserialized
  Familia.debug "[TransientFieldType] WARNING: deserialize called on transient field #{@name}"
  nil
end

#persistent?Boolean

Transient fields are not persisted to database

Returns:

  • (Boolean)

    false - transient fields are never persisted



93
94
95
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 93

def persistent?
  false
end

#serialize(_value, _record = nil) ⇒ nil

Transient fields are not serialized to database

This method should not be called since transient fields are not persisted, but we provide it for completeness.

Parameters:

  • _value (Object)

    The value to serialize

  • _record (Object) (defaults to: nil)

    The record instance

Returns:

  • (nil)

    Always nil since transient fields are not serialized



120
121
122
123
124
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 120

def serialize(_value, _record = nil)
  # Transient fields should never be serialized
  Familia.debug "[TransientFieldType] WARNING: serialize called on transient field #{@name}"
  nil
end

#transient?Boolean

A convenience method that wraps persistent?

Returns:

  • (Boolean)


99
100
101
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 99

def transient?
  !persistent?
end