Sha256: 0fdf698579775aeaf336debd8f598a0f22f6b8ffb317035cf4c1f3176ddac6d4
Contents?: true
Size: 1.2 KB
Versions: 8
Compression:
Stored size: 1.2 KB
Contents
module DataMapper module Support class WeakHash attr_reader :cache def initialize( cache = Hash.new ) @cache = cache @key_map = {} @rev_cache = Hash.new{|h,k| h[k] = {}} @reclaim_value = lambda do |value_id| if @rev_cache.has_key? value_id @rev_cache[value_id].each_key{|key| @cache.delete key} @rev_cache.delete value_id end end @reclaim_key = lambda do |key_id| if @key_map.has_key? key_id @cache.delete @key_map[key_id] end end end def []( key ) value_id = @cache[key] return ObjectSpace._id2ref(value_id) unless value_id.nil? nil rescue RangeError nil end def []=( key, value ) case key when Fixnum, Symbol, true, false key2 = key else key2 = key.dup end @rev_cache[value.object_id][key2] = true @cache[key2] = value.object_id @key_map[key.object_id] = key2 ObjectSpace.define_finalizer(value, @reclaim_value) ObjectSpace.define_finalizer(key, @reclaim_key) end end # class WeakHash end # module Support end # module DataMapper
Version data entries
8 entries across 8 versions & 1 rubygems