Class: Familia::TransientFieldType
- 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.
Instance Attribute Summary
Attributes inherited from FieldType
#fast_method_name, #loggable, #method_name, #name, #on_conflict, #options
Instance Method Summary collapse
-
#category ⇒ Symbol
Category for transient fields.
-
#define_fast_writer(_klass) ⇒ Object
Override fast writer to disable it for transient fields.
-
#define_getter(klass) ⇒ Object
Override getter to unwrap RedactedString values.
-
#define_setter(klass) ⇒ Object
Override setter to wrap values in RedactedString.
-
#deserialize(_value, _record = nil) ⇒ nil
Transient fields are not deserialized from database.
-
#persistent? ⇒ Boolean
Transient fields are not persisted to database.
-
#serialize(_value, _record = nil) ⇒ nil
Transient fields are not serialized to database.
-
#transient? ⇒ Boolean
A convenience method that wraps
persistent?
.
Methods inherited from FieldType
#generated_methods, #initialize, #inspect, #install
Constructor Details
This class inherits a constructor from Familia::FieldType
Instance Method Details
#category ⇒ Symbol
Category for transient fields
105 106 107 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 105 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.
81 82 83 84 85 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 81 def define_fast_writer(_klass) # No fast writer for transient fields since they're not persisted Familia.ld "[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.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 60 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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 35 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.
133 134 135 136 137 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 133 def deserialize(_value, _record = nil) # Transient fields should never be deserialized Familia.ld "[TransientFieldType] WARNING: deserialize called on transient field #{@name}" nil end |
#persistent? ⇒ Boolean
Transient fields are not persisted to database
91 92 93 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 91 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.
118 119 120 121 122 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 118 def serialize(_value, _record = nil) # Transient fields should never be serialized Familia.ld "[TransientFieldType] WARNING: serialize called on transient field #{@name}" nil end |
#transient? ⇒ Boolean
A convenience method that wraps persistent?
97 98 99 |
# File 'lib/familia/features/transient_fields/transient_field_type.rb', line 97 def transient? !persistent? end |