Sha256: 5013925b5aff92abdb26052bd10a18f6cfa45e59a82d986f94093ee69ffce780

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

module Vagrant
  # Register components in a single location that can be queried.
  #
  # This allows certain components (such as guest systems, configuration
  # pieces, etc.) to be registered and queried.
  class Registry
    def initialize
      @actions = {}
    end

    # Register a callable by key.
    #
    # The callable should be given in a block which will be lazily evaluated
    # when the action is needed.
    #
    # If an action by the given name already exists then it will be
    # overwritten.
    def register(key, value=nil, &block)
      block = lambda { value } if value
      @actions[key] = block
    end

    # Get an action by the given key.
    #
    # This will evaluate the block given to `register` and return the resulting
    # action stack.
    def get(key)
      return nil if !@actions.has_key?(key)
      @actions[key].call
    end

    # Iterate over the keyspace.
    def each(&block)
      @actions.each do |key, _|
        yield key, get(key)
      end
    end

    # Converts this registry to a hash
    def to_hash
      result = {}
      self.each do |key, value|
        result[key] = value
      end

      result
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
vagrantup-0.9.3 lib/vagrant/registry.rb
vagrantup-0.9.2 lib/vagrant/registry.rb
vagrantup-0.9.1 lib/vagrant/registry.rb
vagrantup-0.9.0 lib/vagrant/registry.rb
vagrant-0.9.3 lib/vagrant/registry.rb
vagrant-0.9.2 lib/vagrant/registry.rb
vagrant-0.9.1 lib/vagrant/registry.rb
vagrant-0.9.0 lib/vagrant/registry.rb