class R10K::KeyedFactory
This implements a factory by storing classes indexed with a given key and creates objects based on that key.
Attributes
implementations[R]
@!attribute [r] implementations
@return [Hash<Object, Class>] A hash of keys and the associated implementations that this factory can generate.
Public Class Methods
new()
click to toggle source
# File lib/r10k/keyed_factory.rb, line 12 def initialize @implementations = {} end
Public Instance Methods
generate(key, *args)
click to toggle source
# 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}" end end
register(key, klass)
click to toggle source
# File lib/r10k/keyed_factory.rb, line 16 def register(key, klass) if @implementations.has_key?(key) raise DuplicateImplementationError, "Class already registered for #{key}" else @implementations[key] = klass end end
retrieve(key)
click to toggle source
# File lib/r10k/keyed_factory.rb, line 24 def retrieve(key) @implementations[key] end