Sha256: 44e4d9c0a3ea0853547f3989c398f1d2ab0a639180952f05d3c194eddfbd2a60
Contents?: true
Size: 1.46 KB
Versions: 43
Compression:
Stored size: 1.46 KB
Contents
# frozen_string_literal: true module ActiveModel module Type module SerializeCastValue # :nodoc: extend ActiveSupport::Concern module ClassMethods def serialize_cast_value_compatible? return @serialize_cast_value_compatible if defined?(@serialize_cast_value_compatible) @serialize_cast_value_compatible = ancestors.index(instance_method(:serialize_cast_value).owner) <= ancestors.index(instance_method(:serialize).owner) end end module DefaultImplementation def serialize_cast_value(value) value end end def self.included(klass) klass.include DefaultImplementation unless klass.method_defined?(:serialize_cast_value) end 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 def itself_if_serialize_cast_value_compatible self if self.class.serialize_cast_value_compatible? end def initialize(...) super self.class.serialize_cast_value_compatible? # eagerly compute end end end end
Version data entries
43 entries across 43 versions & 6 rubygems