Sha256: 85d35a478c080df5e1c5ae56e8e7eb1ab4ef4822d2576d08b2c658d3749e1e7b

Contents?: true

Size: 1.28 KB

Versions: 32

Compression:

Stored size: 1.28 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 = {}
      @results_cache = {}
    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)
      return @results_cache[key] if @results_cache.has_key?(key)
      @results_cache[key] = @actions[key].call
    end
    alias :[] :get

    # 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

32 entries across 32 versions & 6 rubygems

Version Path
bmhatfield-vagrant-1.0.10 lib/vagrant/registry.rb
bmhatfield-vagrant-1.0.9 lib/vagrant/registry.rb
bmhatfield-vagrant-1.0.8 lib/vagrant/registry.rb
bmhatfield-vagrant-1.0.7 lib/vagrant/registry.rb
vagrantup-1.0.7 lib/vagrant/registry.rb
vagrantup-1.0.6 lib/vagrant/registry.rb
vagrantup-1.0.5 lib/vagrant/registry.rb
vagrantup-1.0.4 lib/vagrant/registry.rb
vagrantup-1.0.3 lib/vagrant/registry.rb
vagrantup-1.0.2 lib/vagrant/registry.rb
vagrantup-1.0.1 lib/vagrant/registry.rb
vagrantup-1.0.0 lib/vagrant/registry.rb
vagrantup-0.9.99.2 lib/vagrant/registry.rb
vagrantup-0.9.99.1 lib/vagrant/registry.rb
vagrantup-0.9.7 lib/vagrant/registry.rb
vagrantup-0.9.6 lib/vagrant/registry.rb
vagrantup-0.9.5 lib/vagrant/registry.rb
vagrantup-0.9.4 lib/vagrant/registry.rb
vagrant-fixed-ssh-1.0.7 lib/vagrant/registry.rb
vagrant-1.0.7 lib/vagrant/registry.rb