Module: Rodbot::Memoize::ClassMethods

Defined in:
lib/rodbot/memoize.rb

Instance Method Summary collapse

Instance Method Details

#memoize(method) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rodbot/memoize.rb', line 49

def memoize(method)
  unmemoized_method = :"_unmemoized_#{method}"
  alias_method unmemoized_method, method
  define_method method do |*args, **kargs, &block|
    if Rodbot::Memoize.suspend? || block
      send(unmemoized_method, *args, **kargs, &block)
    else
      cache = Rodbot::Memoize.cache
      id = object_id.hash ^ method.hash ^ args.hash ^ kargs.hash
      if !Rodbot::Memoize.revisit? && cache.has_key?(id)
        cache[id]
      else
        cache[id] = send(unmemoized_method, *args, **kargs)
      end
    end
  end
  method
end