Class: ConfigBuilder::ClassRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/config_builder/class_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

This defines a registry for classes, and behaves as a factory for registered classes.

Defined Under Namespace

Classes: DuplicateEntry, UnknownEntry

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ClassRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ClassRegistry.



18
19
20
21
# File 'lib/config_builder/class_registry.rb', line 18

def initialize(name)
  @name    = name
  @klasses = {}
end

Instance Method Details

#generate(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate a class instance with the given hash

Parameters:

  • hash (Hash)

    The identifier and options for the new object

Options Hash (hash):

  • type (String)

    The identifier of the class to instantiate

Returns:

  • (Object)

    The generated object



59
60
61
62
63
64
65
66
# File 'lib/config_builder/class_registry.rb', line 59

def generate(hash)
  config = hash.dup
  identifier = config.delete('type')

  klass = retrieve(identifier)

  klass.new_from_hash(hash)
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
# File 'lib/config_builder/class_registry.rb', line 68

def inspect
  "<#{self.class}: (#{@name}) keys: #{@klasses.keys.inspect}>"
end

#register(identifier, klass) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Register a class with a given identifier

Parameters:

  • identifier (Symbol)
  • klass (Class)


29
30
31
32
33
34
35
36
# File 'lib/config_builder/class_registry.rb', line 29

def register(identifier, klass)
  if @klasses[identifier]
    raise DuplicateEntry, :registry => @name,
                          :identifier => identifier.inspect
  else
    @klasses[identifier] = klass
  end
end

#retrieve(identifier) ⇒ Class

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieve a class associated with the specified identifier

Parameters:

  • identifier (Symbol)

Returns:

  • (Class)


43
44
45
46
47
48
49
50
51
# File 'lib/config_builder/class_registry.rb', line 43

def retrieve(identifier)
  if (klass = @klasses[identifier])
    klass
  else
    raise UnknownEntry, :registry  => @name,
                        :identifier => identifier.inspect,
                        :identifiers => @klasses.keys
  end
end