Class: R10K::InstanceCache

Inherits:
Object
  • Object
show all
Defined in:
lib/r10k/instance_cache.rb

Overview

This class implements a generic object memoization container. It caches new objects and returns cached objects based on the instantiation arguments.

Instance Method Summary collapse

Constructor Details

#initialize(klass, method = :new) ⇒ InstanceCache

Initialize a new registry with a given class

Parameters:

  • klass (Class)

    The class to memoize

  • method (Symbol) (defaults to: :new)

    The method name to use when creating objects. Defaults to :new.



12
13
14
15
16
# File 'lib/r10k/instance_cache.rb', line 12

def initialize(klass, method = :new)
  @klass  = klass
  @method = method
  @instances = {}
end

Instance Method Details

#clear!Object

Clear all memoized objects



28
29
30
# File 'lib/r10k/instance_cache.rb', line 28

def clear!
  @instances = {}
end

#generate(*args) ⇒ Object

Create a new object, or return a memoized object.

Parameters:

  • args (*Object)

    The arguments to pass to the initialize method

Returns:

  • (Object)

    A memoized instance of the registered class



23
24
25
# File 'lib/r10k/instance_cache.rb', line 23

def generate(*args)
  @instances[args] ||= @klass.send(@method, *args)
end