Module: Mize::Memoize

Includes:
CacheMethods
Included in:
Module
Defined in:
lib/mize/memoize.rb

Instance Method Summary collapse

Methods included from CacheMethods

#mize_cache_clear, #mize_cache_clear_name

Instance Method Details

#memoize(method: nil, function: nil, freeze: false, store_nil: true) ⇒ Object

Memoize either a method or a function. In the former case the memoized results do NOT ONLY depend on the arguments, but ALSO on the object the method is called on. In the latter the memoized results ONLY depend on the arguments given to the function. If the freeze argument is true, the result is frozen if possible to make it immutable. If the store_nil argument is false do not store nil results and always recompute otherwise don’t which is the default.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mize/memoize.rb', line 15

def memoize(method: nil, function: nil, freeze: false, store_nil: true)
  Mize::MUTEX.synchronize do
    if method && function
      raise ArgumentError, 'memoize a method xor a function'
    elsif method
      wrap_method method, freeze: freeze, store_nil: store_nil
    elsif function
      wrap_method function, function: true, freeze: freeze, store_nil: store_nil
    else
      raise ArgumentError, 'missing keyword: method/function'
    end
  end
end