Sha256: 49f30de6c2511fa05698ba262b02afe13c69115912af1a7727134c7e6ae50ad4
Contents?: true
Size: 1.43 KB
Versions: 5
Compression:
Stored size: 1.43 KB
Contents
require 'data_mapper/support/weak_hash' module DataMapper # Tracks objects to help ensure that each object gets loaded only once. # See: http://www.martinfowler.com/eaaCatalog/identityMap.html class IdentityMap def initialize # WeakHash is much more expensive, and not necessary if the IdentityMap is tied to Session instead of Database. # @cache = Hash.new { |h,k| h[k] = Support::WeakHash.new } @cache = Hash.new { |h,k| h[k] = Hash.new } end # Pass a Class and a key, and to retrieve an instance. # If the instance isn't found, nil is returned. def get(klass, key) @cache[mapped_class(klass)][key] end # Pass an instance to add it to the IdentityMap. # The instance must have an assigned key. def set(instance) instance_key = instance.key raise "Can't store an instance with a nil key in the IdentityMap" if instance_key.nil? @cache[mapped_class(instance.class)][instance_key] = instance end # Remove an instance from the IdentityMap. def delete(instance) @cache[mapped_class(instance.class)].delete(instance.key) end # Clears a particular set of classes from the IdentityMap. def clear!(klass) @cache.delete(klass) end private def mapped_class(klass) if klass.superclass == DataMapper::Base klass else mapped_class(klass.superclass) end end end end
Version data entries
5 entries across 5 versions & 1 rubygems