Skip to content

Commit

Permalink
Wrap WeakRefs in object with custom #inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 3, 2016
1 parent 2b89154 commit e153db3
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/nanoc/base/memoization.rb
Expand Up @@ -8,6 +8,20 @@ module Nanoc::Int
# @since 3.2.0
module Memoization
class Wrapper
attr_reader :ref

def initialize(ref)
@ref = ref
end

def inspect
@ref.inspect
rescue WeakRef::RefError
'<weak ref collected>'
end
end

class Value
attr_reader :value

def initialize(value)
Expand Down Expand Up @@ -63,15 +77,15 @@ def memoize(method_name)
if method_cache.key?(args)
value =
begin
method_cache[args].value
method_cache[args].ref.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))
method_cache[args] = Wrapper.new(WeakRef.new(Value.new(r)))
end
else
value
Expand Down

0 comments on commit e153db3

Please sign in to comment.