This class implements a generic object memoization container. It caches new objects and returns cached objects based on the instantiation arguments.
Initialize a new registry with a given class
@param klass [Class] The class to memoize @param method [Symbol] The method name to use when creating objects.
Defaults to :new.
# File lib/r10k/instance_cache.rb, line 12 def initialize(klass, method = :new) @klass = klass @method = method @instances = {} end
Clear all memoized objects
# File lib/r10k/instance_cache.rb, line 28 def clear! @instances = {} end
Create a new object, or return a memoized object.
@param args [*Object] The arguments to pass to the initialize method
@return [Object] A memoized instance of the registered class
# File lib/r10k/instance_cache.rb, line 23 def generate(*args) @instances[args] ||= @klass.send(@method, *args) end