Sha256: 58af5726f3106e5acb4b062f5e62b2290228abead657902a83f7830388c9ee9e

Contents?: true

Size: 1.37 KB

Versions: 28

Compression:

Stored size: 1.37 KB

Contents

module ActiveRecord
  module Type
    class Serialized < DelegateClass(ActiveModel::Type::Value) # :nodoc:
      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)
        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

28 entries across 28 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/type/serialized.rb
activerecord-5.0.7.1 lib/active_record/type/serialized.rb
activerecord-5.0.7 lib/active_record/type/serialized.rb
activerecord-5.0.6 lib/active_record/type/serialized.rb
activerecord-5.0.6.rc1 lib/active_record/type/serialized.rb
activerecord-5.0.5 lib/active_record/type/serialized.rb
activerecord-5.0.5.rc2 lib/active_record/type/serialized.rb
activerecord-5.0.5.rc1 lib/active_record/type/serialized.rb
activerecord-5.0.4 lib/active_record/type/serialized.rb
activerecord-5.0.4.rc1 lib/active_record/type/serialized.rb
activerecord-5.0.3 lib/active_record/type/serialized.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/type/serialized.rb
activerecord-5.0.2 lib/active_record/type/serialized.rb
activerecord-5.0.2.rc1 lib/active_record/type/serialized.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/type/serialized.rb