Sha256: d90d43fbae2ab6cb9535c6af14ee90ccc2b76ab3bde2b17938ea1a59322d01c0

Contents?: true

Size: 1.8 KB

Versions: 22

Compression:

Stored size: 1.8 KB

Contents

require 'vagrant/errors'

module ConfigBuilder

  # This defines a registry for classes, and behaves as a factory for registered classes.
  #
  # @api private
  class ClassRegistry

    class UnknownEntry < Vagrant::Errors::VagrantError
      error_key('unknown_entry', 'config_builder.class_registry')
    end

    class DuplicateEntry < Vagrant::Errors::VagrantError
      error_key('duplicate_entry', 'config_builder.class_registry')
    end

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

    # Register a class with a given identifier
    #
    # @param identifier [Symbol]
    # @param klass [Class]
    #
    # @return [void]
    def register(identifier, klass)
      if @klasses[identifier]
        raise DuplicateEntry, :registry => @name,
                              :identifier => identifier.inspect
      else
        @klasses[identifier] = klass
      end
    end

    # Retrieve a class associated with the specified identifier
    #
    # @param identifier [Symbol]
    #
    # @return [Class]
    def retrieve(identifier)
      if (klass = @klasses[identifier])
        klass
      else
        raise UnknownEntry, :registry  => @name,
                            :identifier => identifier.inspect,
                            :identifiers => @klasses.keys
      end
    end

    # Generate a class instance with the given hash
    #
    # @param hash [Hash] The identifier and options for the new object
    # @option hash type [String] The identifier of the class to instantiate
    #
    # @return [Object] The generated object
    def generate(hash)
      config = hash.dup
      identifier = config.delete('type')

      klass = retrieve(identifier)

      klass.new_from_hash(hash)
    end

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

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
vagrant-config_builder-1.3.0 lib/config_builder/class_registry.rb
vagrant-config_builder-1.2.0 lib/config_builder/class_registry.rb
vagrant-config_builder-1.1.0 lib/config_builder/class_registry.rb
vagrant-config_builder-1.0.0 lib/config_builder/class_registry.rb
vagrant-config_builder-1.0.0.rc1 lib/config_builder/class_registry.rb
vagrant-config_builder-0.15.1 lib/config_builder/class_registry.rb
vagrant-config_builder-0.15.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.14.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.13.0 lib/config_builder/class_registry.rb
vagrant-masonry-0.13.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.12.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.11.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.10.1 lib/config_builder/class_registry.rb
vagrant-config_builder-0.10.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.9.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.8.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.7.1 lib/config_builder/class_registry.rb
vagrant-config_builder-0.7.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.6.0 lib/config_builder/class_registry.rb
vagrant-config_builder-0.5.0 lib/config_builder/class_registry.rb