Module: AIXM::Memoize::ClassMethods

Defined in:
lib/aixm/memoize.rb

Instance Method Summary collapse

Instance Method Details

#memoize(method) ⇒ Object


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

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