Module: AIXM::Concerns::Memoize::ClassMethods

Defined in:
lib/aixm/concerns/memoize.rb

Instance Method Summary collapse

Instance Method Details

#memoize(method) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aixm/concerns/memoize.rb', line 56

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