Sha256: c968683e8664a8dec16bf38424ad4e0e9ba78717a18911e004bdf349f19fc4a8

Contents?: true

Size: 1.45 KB

Versions: 17

Compression:

Stored size: 1.45 KB

Contents

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

      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

17 entries across 17 versions & 1 rubygems

Version Path
activerecord-5.1.6.2 lib/active_record/type/serialized.rb
activerecord-5.1.6.1 lib/active_record/type/serialized.rb
activerecord-5.1.6 lib/active_record/type/serialized.rb
activerecord-5.1.5 lib/active_record/type/serialized.rb
activerecord-5.1.5.rc1 lib/active_record/type/serialized.rb
activerecord-5.1.4 lib/active_record/type/serialized.rb
activerecord-5.1.4.rc1 lib/active_record/type/serialized.rb
activerecord-5.1.3 lib/active_record/type/serialized.rb
activerecord-5.1.3.rc3 lib/active_record/type/serialized.rb
activerecord-5.1.3.rc2 lib/active_record/type/serialized.rb
activerecord-5.1.3.rc1 lib/active_record/type/serialized.rb
activerecord-5.1.2 lib/active_record/type/serialized.rb
activerecord-5.1.2.rc1 lib/active_record/type/serialized.rb
activerecord-5.1.1 lib/active_record/type/serialized.rb
activerecord-5.1.0 lib/active_record/type/serialized.rb
activerecord-5.1.0.rc2 lib/active_record/type/serialized.rb
activerecord-5.1.0.rc1 lib/active_record/type/serialized.rb