Module: ActiveModel::Type::SerializeCastValue

Extended by:
ActiveSupport::Concern
Included in:
Value
Defined in:
lib/active_model/type/serialize_cast_value.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, DefaultImplementation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



21
22
23
# File 'lib/active_model/type/serialize_cast_value.rb', line 21

def self.included(klass)
  klass.include DefaultImplementation unless klass.method_defined?(:serialize_cast_value)
end

.serialize(type, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_model/type/serialize_cast_value.rb', line 25

def self.serialize(type, value)
  # Using `type.equal?(type.itself_if_...)` is a performant way to also
  # ensure that `type` is not just a DelegateClass instance (e.g.
  # ActiveRecord::Type::Serialized) unintentionally delegating
  # SerializeCastValue methods.
  if type.equal?((type.itself_if_serialize_cast_value_compatible rescue nil))
    type.serialize_cast_value(value)
  else
    type.serialize(value)
  end
end

Instance Method Details

#initializeObject



41
42
43
44
# File 'lib/active_model/type/serialize_cast_value.rb', line 41

def initialize(...)
  super
  self.class.serialize_cast_value_compatible? # eagerly compute
end

#itself_if_serialize_cast_value_compatibleObject



37
38
39
# File 'lib/active_model/type/serialize_cast_value.rb', line 37

def itself_if_serialize_cast_value_compatible
  self if self.class.serialize_cast_value_compatible?
end