Sha256: dfdd1d08258daf223b1b499eba1bc6bcb45f98e0f054c1cad3e47abf99ac6f7d

Contents?: true

Size: 1.6 KB

Versions: 45

Compression:

Stored size: 1.6 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

      def inspect
        Kernel.instance_method(:inspect).bind(self).call
      end

      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

      private

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

        def encoded(value)
          unless default_value?(value)
            coder.dump(value)
          end
        end
    end
  end
end

Version data entries

45 entries across 45 versions & 6 rubygems

Version Path
activerecord-5.2.8.1 lib/active_record/type/serialized.rb
activerecord-5.2.8 lib/active_record/type/serialized.rb
activerecord-5.2.7.1 lib/active_record/type/serialized.rb
activerecord-5.2.7 lib/active_record/type/serialized.rb
activerecord-5.2.6.3 lib/active_record/type/serialized.rb
activerecord-5.2.6.2 lib/active_record/type/serialized.rb
activerecord-5.2.6.1 lib/active_record/type/serialized.rb
activerecord-5.2.6 lib/active_record/type/serialized.rb
activerecord-5.2.4.6 lib/active_record/type/serialized.rb
activerecord-5.2.5 lib/active_record/type/serialized.rb
activerecord-5.2.4.5 lib/active_record/type/serialized.rb
activerecord-5.2.4.4 lib/active_record/type/serialized.rb
activerecord-5.2.4.3 lib/active_record/type/serialized.rb
activerecord-6.0.2.2 lib/active_record/type/serialized.rb
activerecord-5.2.4.2 lib/active_record/type/serialized.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/type/serialized.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/type/serialized.rb
activerecord-6.0.2.1 lib/active_record/type/serialized.rb
activerecord-5.2.4.1 lib/active_record/type/serialized.rb
activerecord-6.0.2 lib/active_record/type/serialized.rb