module FondMemos

foo = Foo.new foo.bar # expensive foo.bar # fast foo.forget(:bar) foo.bar # expensive again (why did you forget?) foo.bar # fast again (that’s better)

If you memoize a method with arguments, the hash method will be called on each argument to generate a key to use in FondMemo’s internal hash. To ensure safe and responsible memoizing, it is strongly encouraged to ensure your memoized method arguments have logical hash values.

May you always have fond memos of using this gem!

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/fond_memos.rb, line 32
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

forget(method) click to toggle source

Calling this will remove the caching instance variable for the method. In the case of multi argument methods, all values will be forgotten. @param method [Symbol] the method to forget

# File lib/fond_memos.rb, line 39
def forget(method)
  remove_instance_variable(Internal.var_name(method))
end

Private Instance Methods

_fond_fetch(var_name, original_method) click to toggle source
# File lib/fond_memos.rb, line 45
def _fond_fetch(var_name, original_method)
  return instance_variable_get(var_name) if instance_variable_defined?(var_name)
  instance_variable_set(var_name, original_method.bind(self).call)
end
_fond_multi_fetch(var_name, original_method, args) click to toggle source
# File lib/fond_memos.rb, line 50
def _fond_multi_fetch(var_name, original_method, args)
  instance_variable_set(var_name, {}) unless instance_variable_defined?(var_name)
  hash = instance_variable_get(var_name)
  key = args.map(&:hash)
  if hash.key?(key)
    hash[key]
  else
    hash[key] = original_method.bind(self).call(*args)
  end
end