Class: R10K::InstanceCache
- Inherits:
-
Object
- Object
- R10K::InstanceCache
- 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
-
#clear! ⇒ Object
Clear all memoized objects.
-
#generate(*args) ⇒ Object
Create a new object, or return a memoized object.
-
#initialize(klass, method = :new) ⇒ InstanceCache
constructor
Initialize a new registry with a given class.
Constructor Details
#initialize(klass, method = :new) ⇒ InstanceCache
Initialize a new registry with a given class
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.
23 24 25 |
# File 'lib/r10k/instance_cache.rb', line 23 def generate(*args) @instances[args] ||= @klass.send(@method, *args) end |