blob: d6096e606ecf3073e9fb986bf278fa380a2ab939 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
module ActiveSupport
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
WeakHash = ::Weakling::WeakHash
else
class WeakHash
def initialize(cache = Hash.new)
@cache = cache
@key_map = {}
@rev_cache = Hash.new{|h,k| h[k] = {}}
@reclaim_value = lambda do |value_id|
if value = @rev_cache.delete(value_id)
value.each_key{|key| @cache.delete key}
end
end
end
def [](key)
value_id = @cache[key]
value_id && ObjectSpace._id2ref(value_id)
rescue RangeError
nil
end
def []=(key, value)
@rev_cache[value.object_id][key] = true
@cache[key] = value.object_id
@key_map[key.object_id] = key
ObjectSpace.define_finalizer(value, @reclaim_value)
end
def clear
@cache.clear
end
def delete(key)
@cache.delete(key)
end
end
end
end
|