Class: Familia::FieldType
- Inherits:
-
Object
- Object
- Familia::FieldType
- Defined in:
- lib/familia/field_type.rb
Overview
Base class for all field types in Familia
Field types encapsulate the behavior for different kinds of fields, including how their getter/setter methods are defined and how values are serialized/deserialized.
Direct Known Subclasses
EncryptedFieldType, Familia::Features::ExternalIdentifier::ExternalIdentifierFieldType, Familia::Features::ObjectIdentifier::ObjectIdentifierFieldType, TransientFieldType
Instance Attribute Summary collapse
-
#fast_method_name ⇒ Object
readonly
Returns the value of attribute fast_method_name.
-
#loggable ⇒ Object
readonly
Returns the value of attribute loggable.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#on_conflict ⇒ Object
readonly
Returns the value of attribute on_conflict.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#category ⇒ Symbol
The category for this field type (used for filtering).
-
#define_fast_writer(klass) ⇒ Object
Define the fast writer method on the target class.
-
#define_getter(klass) ⇒ Object
Define the getter method on the target class.
-
#define_setter(klass) ⇒ Object
Define the setter method on the target class.
-
#deserialize(value, _record = nil) ⇒ Object
Deserialize a value from database storage.
-
#generated_methods ⇒ Array<Symbol>
Returns all method names generated for this field (used for conflict detection).
-
#initialize(name, as: name, fast_method: :"#{name}!", on_conflict: :raise, loggable: true, **options) ⇒ FieldType
constructor
Initialize a new field type.
-
#inspect ⇒ String
(also: #to_s)
Enhanced inspection output for debugging.
-
#install(klass) ⇒ Object
Install this field type on a class.
-
#persistent? ⇒ Boolean
Whether this field should be persisted to the database.
-
#serialize(value, _record = nil) ⇒ Object
Serialize a value for database storage.
- #transient? ⇒ Boolean
Constructor Details
#initialize(name, as: name, fast_method: :"#{name}!", on_conflict: :raise, loggable: true, **options) ⇒ FieldType
Initialize a new field type
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/familia/field_type.rb', line 47 def initialize(name, as: name, fast_method: :"#{name}!", on_conflict: :raise, loggable: true, **) @name = name.to_sym @method_name = as == false ? nil : as.to_sym @fast_method_name = fast_method == false ? nil : fast_method&.to_sym # Validate fast method name format if @fast_method_name && !@fast_method_name.to_s.end_with?('!') raise ArgumentError, "Fast method name must end with '!' (got: #{@fast_method_name})" end @on_conflict = on_conflict @loggable = loggable @options = end |
Instance Attribute Details
#fast_method_name ⇒ Object (readonly)
Returns the value of attribute fast_method_name.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def fast_method_name @fast_method_name end |
#loggable ⇒ Object (readonly)
Returns the value of attribute loggable.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def loggable @loggable end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def method_name @method_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def name @name end |
#on_conflict ⇒ Object (readonly)
Returns the value of attribute on_conflict.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def on_conflict @on_conflict end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
30 31 32 |
# File 'lib/familia/field_type.rb', line 30 def @options end |
Instance Method Details
#category ⇒ Symbol
The category for this field type (used for filtering)
203 204 205 |
# File 'lib/familia/field_type.rb', line 203 def category :field end |
#define_fast_writer(klass) ⇒ Object
Define the fast writer method on the target class
Fast methods provide direct database access for immediate persistence. Subclasses can override this to customize fast method behavior.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/familia/field_type.rb', line 150 def define_fast_writer(klass) return unless @fast_method_name&.to_s&.end_with?('!') field_name = @name method_name = @method_name fast_method_name = @fast_method_name handle_method_conflict(klass, fast_method_name) do klass.define_method fast_method_name do |*args| raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0 or 1)" if args.size > 1 val = args.first # If no value provided, return current stored value return hget(field_name) if val.nil? begin # Trace the operation if debugging is enabled Familia.trace :FAST_WRITER, nil, "#{field_name}: #{val.inspect}" if Familia.debug? # Convert value for database storage prepared = serialize_value(val) Familia.ld "[FieldType#define_fast_writer] #{fast_method_name} val: #{val.class} prepared: #{prepared.class}" # Use the setter method to update instance variable send(:"#{method_name}=", val) if method_name # Persist to database immediately ret = hset(field_name, prepared) ret.zero? || ret.positive? rescue Familia::Problem => e raise "#{fast_method_name} method failed: #{e.}", e.backtrace end end end end |
#define_getter(klass) ⇒ Object
Define the getter method on the target class
Subclasses can override this to customize getter behavior. The default implementation creates a simple attr_reader equivalent.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/familia/field_type.rb', line 94 def define_getter(klass) field_name = @name method_name = @method_name handle_method_conflict(klass, method_name) do klass.define_method method_name do instance_variable_get(:"@#{field_name}") end end end |
#define_setter(klass) ⇒ Object
Define the setter method on the target class
Subclasses can override this to customize setter behavior. The default implementation creates a simple attr_writer equivalent.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/familia/field_type.rb', line 112 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) # If this field is the identifier and object_identifier feature is loaded, # update objid_lookup mapping when identifier is set after objid generation if respond_to?(:objid) && self.class.respond_to?(:identifier_field) && self.class.identifier_field == field_name && self.class.respond_to?(:objid_lookup) current_objid = instance_variable_get(:@objid) self.class.objid_lookup[current_objid] = value if current_objid && value end # If this field is the identifier and external_identifier feature is loaded, # update extid_lookup mapping when identifier is set after extid generation if respond_to?(:extid) && self.class.respond_to?(:identifier_field) && self.class.identifier_field == field_name && self.class.respond_to?(:extid_lookup) current_extid = instance_variable_get(:@extid) self.class.extid_lookup[current_extid] = value if current_extid && value end end end end |
#deserialize(value, _record = nil) ⇒ Object
Deserialize a value from database storage
Subclasses can override this to customize deserialization. The default implementation passes values through unchanged.
229 230 231 |
# File 'lib/familia/field_type.rb', line 229 def deserialize(value, _record = nil) value end |
#generated_methods ⇒ Array<Symbol>
Returns all method names generated for this field (used for conflict detection)
237 238 239 |
# File 'lib/familia/field_type.rb', line 237 def generated_methods [@method_name, @fast_method_name].compact end |
#inspect ⇒ String Also known as: to_s
Enhanced inspection output for debugging
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/familia/field_type.rb', line 245 def inspect attributes = [ "name=#{@name}", "method_name=#{@method_name}", "fast_method_name=#{@fast_method_name}", "on_conflict=#{@on_conflict}", "category=#{category}", ] "#<#{self.class.name} #{attributes.join(' ')}>" end |
#install(klass) ⇒ Object
Install this field type on a class
This method defines all necessary methods on the target class and registers the field type for later reference.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/familia/field_type.rb', line 69 def install(klass) if @method_name # For skip strategy, check for any method conflicts first if @on_conflict == :skip has_getter_conflict = klass.method_defined?(@method_name) || klass.private_method_defined?(@method_name) has_setter_conflict = klass.method_defined?(:"#{@method_name}=") || klass.private_method_defined?(:"#{@method_name}=") # If either getter or setter conflicts, skip the whole field return if has_getter_conflict || has_setter_conflict end define_getter(klass) define_setter(klass) end define_fast_writer(klass) if @fast_method_name end |
#persistent? ⇒ Boolean
Whether this field should be persisted to the database
191 192 193 |
# File 'lib/familia/field_type.rb', line 191 def persistent? true end |
#serialize(value, _record = nil) ⇒ Object
Serialize a value for database storage
Subclasses can override this to customize serialization. The default implementation passes values through unchanged.
216 217 218 |
# File 'lib/familia/field_type.rb', line 216 def serialize(value, _record = nil) value end |
#transient? ⇒ Boolean
195 196 197 |
# File 'lib/familia/field_type.rb', line 195 def transient? !persistent? end |