Class: Anchormodel::ActiveModelTypeValueSingle
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- Anchormodel::ActiveModelTypeValueSingle
- Defined in:
- lib/anchormodel/active_model_type_value_single.rb
Overview
ActiveModel type adapter for single-value anchormodel attributes.
Translates between the in-memory Ruby form (an Anchormodel instance or nil) and
the on-disk form (a String key in the DB column). Registered with AR via
model_class.attribute(name, ActiveModelTypeValueSingle.new(attribute)) by
Util.install_methods_in_model.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
Instance Method Summary collapse
-
#cast(value) ⇒ Anchormodel?
Coerces an input value into an Anchormodel instance (or nil).
-
#changed_in_place?(raw_old_value, value) ⇒ Boolean
private
Used by AR's dirty tracking to detect in-place mutation.
-
#initialize(attribute) ⇒ ActiveModelTypeValueSingle
constructor
A new instance of ActiveModelTypeValueSingle.
-
#serializable?(value) ⇒ Boolean
Reports whether
valueis a shape that #serialize would accept. -
#serialize(value) ⇒ String?
Converts an Anchormodel-shaped value into the String key stored in the DB.
-
#type ⇒ Symbol
:anchormodel— the type identifier.
Constructor Details
#initialize(attribute) ⇒ ActiveModelTypeValueSingle
Returns a new instance of ActiveModelTypeValueSingle.
15 16 17 18 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 15 def initialize(attribute) super() @attribute = attribute end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
12 13 14 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 12 def attribute @attribute end |
Instance Method Details
#cast(value) ⇒ Anchormodel?
Coerces an input value into an Anchormodel instance (or nil). Used by Rails when reading from the DB, when assigning via mass-assignment, and for dirty-tracking.
31 32 33 34 35 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 31 def cast(value) value = value.presence return value if value.is_a?(@attribute.anchormodel_class) return @attribute.anchormodel_class.find(value) end |
#changed_in_place?(raw_old_value, value) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used by AR's dirty tracking to detect in-place mutation.
60 61 62 63 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 60 def changed_in_place?(raw_old_value, value) old_value = deserialize(raw_old_value) old_value != value end |
#serializable?(value) ⇒ Boolean
Reports whether value is a shape that #serialize would accept. Used by AR's
Arel::Nodes::HomogeneousIn to gate which array elements get bound into IN clauses.
Returns true for any String/Symbol/nil/Anchormodel instance — actual key validation
is deferred to #serialize.
54 55 56 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 54 def serializable?(value) scalar_serializable?(value) end |
#serialize(value) ⇒ String?
Converts an Anchormodel-shaped value into the String key stored in the DB.
43 44 45 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 43 def serialize(value) serialize_scalar(value) end |
#type ⇒ Symbol
Returns :anchormodel — the type identifier.
21 22 23 |
# File 'lib/anchormodel/active_model_type_value_single.rb', line 21 def type :anchormodel end |