Class: R10K::KeyedFactory

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

Overview

This implements a factory by storing classes indexed with a given key and creates objects based on that key.

Defined Under Namespace

Classes: DuplicateImplementationError, UnknownImplementationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeyedFactory

Returns a new instance of KeyedFactory.



12
13
14
# File 'lib/r10k/keyed_factory.rb', line 12

def initialize
  @implementations = {}
end

Instance Attribute Details

#implementationsObject (readonly)

Returns the value of attribute implementations.



10
11
12
# File 'lib/r10k/keyed_factory.rb', line 10

def implementations
  @implementations
end

Instance Method Details

#generate(key, *args) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/r10k/keyed_factory.rb', line 28

def generate(key, *args)
  if (impl = @implementations[key])
    impl.new(*args)
  else
    raise UnknownImplementationError, _("No class registered for %{key}") % {key: key}
  end
end

#register(key, klass) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/r10k/keyed_factory.rb', line 16

def register(key, klass)
  if @implementations.has_key?(key)
    raise DuplicateImplementationError, _("Class already registered for %{key}") % {key: key}
  else
    @implementations[key] = klass
  end
end

#retrieve(key) ⇒ Object



24
25
26
# File 'lib/r10k/keyed_factory.rb', line 24

def retrieve(key)
  @implementations[key]
end