Sha256: 244ee75d689f2f88d8d034fc4f71617220bdf67cc2f986e9651671d3ef7ff141

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

# -*- encoding : utf-8 -*-
module RecordMarshal
  class << self
    # dump ActiveRecord instace with only attributes.
    # ["User",
    #  {"id"=>30,
    #  "email"=>"dddssddd@gmail.com",
    #  "created_at"=>2012-07-25 18:25:57 UTC
    #  }
    # ]

    def dump(record)
      [
       record.class.name,
       record.attributes
      ]
    end

    # load a cached record
    def load(serialized)
      return unless serialized
      #fix issues 19
      #fix 2.1.2 object.changed? ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Hash, but was a String. -- "{:a=>\"t\", :b=>\"x\"}"
      #fix 2.1.4 object.changed? is true
      #fix Rails 4.2 is deprecating `serialized_attributes` without replacement to Rails 5 is deprecating `serialized_attributes` without replacement
      klass, attributes = serialized[0].constantize, serialized[1]
      if ::ActiveRecord::VERSION::STRING < '4.2.0'
        klass.serialized_attributes.each do |k, v|
          next if attributes[k].nil? || attributes[k].is_a?(String)
          attributes[k] = attributes[k].serialized_value if attributes[k].respond_to?(:unserialize)
        end
      else
        klass.columns.select{|t| t.cast_type.is_a?(::ActiveRecord::Type::Serialized) }.each do |c|
          name, coder = c.name, c.cast_type.coder
          next if attributes[name].nil? || attributes[name].is_a?(String)
          attributes[name] = coder.dump(attributes[name]) if attributes[name].is_a?(coder.object_class)
        end
      end
      klass.instantiate(attributes)
    end

    def load_multi(serializeds)
      serializeds.map{|serialized| load(serialized)}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
second_level_cache-2.1.5 lib/second_level_cache/record_marshal.rb