Sha256: 4e65a7f2be058b2d7efe7a4a7e9a6750523c7d207bb0cd3831ae579bc50a0c97

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module ActiveRecord
  module Type
    class Serialized < DelegateClass(Type::Value) # :nodoc:
      include Mutable
      include Decorator

      attr_reader :subtype, :coder

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

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

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

      def changed_in_place?(raw_old_value, value)
        return false if value.nil?
        subtype.changed_in_place?(raw_old_value, coder.dump(value))
      end

      def accessor
        ActiveRecord::Store::IndifferentHashAccessor
      end

      def init_with(coder)
        @coder = coder['coder']
        super
      end

      def encode_with(coder)
        coder['coder'] = @coder
        super
      end

      private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord-4.2.0 lib/active_record/type/serialized.rb
activerecord-4.2.0.rc3 lib/active_record/type/serialized.rb
activerecord-4.2.0.rc2 lib/active_record/type/serialized.rb
activerecord-4.2.0.rc1 lib/active_record/type/serialized.rb