Class: Anchormodel::ActiveModelTypeValueSingle

Inherits:
ActiveModel::Type::Value
  • Object
show all
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

ActiveModelTypeValueMulti

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ ActiveModelTypeValueSingle

Returns a new instance of ActiveModelTypeValueSingle.

Parameters:



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

#attributeObject (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.

Parameters:

  • value (String, Symbol, Anchormodel, nil)

    DB value, user input, or already-cast instance.

Returns:

  • (Anchormodel, nil)

    The matching Anchormodel instance, or nil for blank input.

Raises:



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.

Returns:

  • (Boolean)


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.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


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.

Parameters:

Returns:

  • (String, nil)

    The key as a String, or nil for blank input.

Raises:

  • (Anchormodel::InvalidKey)

    for unknown String/Symbol keys.

  • (RuntimeError)

    for any other input type (e.g. Array, Integer).



43
44
45
# File 'lib/anchormodel/active_model_type_value_single.rb', line 43

def serialize(value)
  serialize_scalar(value)
end

#typeSymbol

Returns :anchormodel — the type identifier.

Returns:

  • (Symbol)

    :anchormodel — the type identifier.



21
22
23
# File 'lib/anchormodel/active_model_type_value_single.rb', line 21

def type
  :anchormodel
end