Skip to content

Commit

Permalink
Fix WeakRef::RefError
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jun 8, 2016
1 parent 4fb2eb5 commit a949747
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/nanoc/base/memoization.rb
Expand Up @@ -15,6 +15,8 @@ def initialize(value)
end
end

NONE = Object.new

# Memoizes the method with the given name. The modified method will cache
# the results of the original method, so that calling a method twice with
# the same arguments will short-circuit and return the cached results
Expand Down Expand Up @@ -57,12 +59,22 @@ def memoize(method_name)
@__memoization_cache[method_name] ||= {}
method_cache = @__memoization_cache[method_name]

if method_cache.key?(args) && method_cache[args].weakref_alive?
method_cache[args].value
else
value = NONE
if method_cache.key?(args)
value =
begin
method_cache[args].value
rescue WeakRef::RefError
NONE
end
end

if value.equal?(NONE)
send(original_method_name, *args).tap do |r|
method_cache[args] = WeakRef.new(Wrapper.new(r))
end
else
value
end
end
end
Expand Down

0 comments on commit a949747

Please sign in to comment.