Sha256: 74e45a046146b99dfe6d869587ee34b1538adfcb21fa9cb8f52cfcf945ce977b

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Type
    class Serialized < DelegateClass(ActiveModel::Type::Value) # :nodoc:
      undef to_yaml if method_defined?(:to_yaml)

      include ActiveModel::Type::Helpers::Mutable

      attr_reader :subtype, :coder

      def initialize(subtype, coder)
        @subtype = subtype
        @coder = coder
        super(subtype)
      end

      def deserialize(value)
        if default_value?(value)
          value
        else
          coder.load(super)
        end
      end

      def serialize(value)
        return if value.nil?
        unless default_value?(value)
          super coder.dump(value)
        end
      end

      define_method(:inspect, Kernel.instance_method(:inspect))

      def changed_in_place?(raw_old_value, value)
        return false if value.nil?
        raw_new_value = encoded(value)
        raw_old_value.nil? != raw_new_value.nil? ||
          subtype.changed_in_place?(raw_old_value, raw_new_value)
      end

      def accessor
        ActiveRecord::Store::IndifferentHashAccessor
      end

      def assert_valid_value(value)
        if coder.respond_to?(:assert_valid_value)
          coder.assert_valid_value(value, action: "serialize")
        end
      end

      def force_equality?(value)
        coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
      end

      def serialized? # :nodoc:
        true
      end

      private
        def default_value?(value)
          value == coder.load(nil)
        end

        def encoded(value)
          return if default_value?(value)
          payload = coder.dump(value)
          if payload && @subtype.binary?
            ActiveModel::Type::Binary::Data.new(payload)
          else
            payload
          end
        end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
activerecord-8.0.0 lib/active_record/type/serialized.rb
activerecord-7.2.2 lib/active_record/type/serialized.rb
activerecord-8.0.0.rc2 lib/active_record/type/serialized.rb
activerecord-7.2.1.2 lib/active_record/type/serialized.rb
activerecord-8.0.0.rc1 lib/active_record/type/serialized.rb
activerecord-7.2.1.1 lib/active_record/type/serialized.rb
activerecord-8.0.0.beta1 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha2 lib/active_record/type/serialized.rb
omg-activerecord-8.0.0.alpha1 lib/active_record/type/serialized.rb
activerecord-7.2.1 lib/active_record/type/serialized.rb
activerecord-7.2.0 lib/active_record/type/serialized.rb
activerecord-7.2.0.rc1 lib/active_record/type/serialized.rb
activerecord-7.2.0.beta3 lib/active_record/type/serialized.rb
activerecord-7.2.0.beta2 lib/active_record/type/serialized.rb
activerecord-7.2.0.beta1 lib/active_record/type/serialized.rb