Sha256: a9c082671630da5f3d08328156649a30675764d5938f3c9bdb525755bd4d75a6

Contents?: true

Size: 1014 Bytes

Versions: 1

Compression:

Stored size: 1014 Bytes

Contents

module ActiveRecord
  module Type
    class Serialized < SimpleDelegator # :nodoc:
      include Mutable

      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 accessor
        ActiveRecord::Store::IndifferentHashAccessor
      end

      def init_with(coder)
        @subtype = coder['subtype']
        @coder = coder['coder']
        __setobj__(@subtype)
      end

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

      private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-4.2.0.beta1 lib/active_record/type/serialized.rb