class Vagrant::Registry

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, lazily.

Public Class Methods

new() click to toggle source
# File lib/vagrant/registry.rb, line 7
def initialize
  @items = {}
  @results_cache = {}
end

Public Instance Methods

[](key)
Alias for: get
__internal_state() click to toggle source
# File lib/vagrant/registry.rb, line 103
def __internal_state
  {
    items: @items,
    results_cache: @results_cache
  }
end
each() { |key, get(key)| ... } click to toggle source

Iterate over the keyspace.

# File lib/vagrant/registry.rb, line 47
def each(&block)
  @items.each do |key, _|
    yield key, get(key)
  end
end
empty?() click to toggle source

Checks if this registry has any items.

@return [Boolean]

# File lib/vagrant/registry.rb, line 73
def empty?
  @items.keys.empty?
end
get(key) click to toggle source

Get a value by the given key.

This will evaluate the block given to ‘register` and return the resulting value.

# File lib/vagrant/registry.rb, line 24
def get(key)
  return nil if !@items.key?(key)
  return @results_cache[key] if @results_cache.key?(key)
  @results_cache[key] = @items[key].call
end
Also aliased as: []
has_key?(key)
Alias for: key?
key?(key) click to toggle source

Checks if the given key is registered with the registry.

@return [Boolean]

# File lib/vagrant/registry.rb, line 34
def key?(key)
  @items.key?(key)
end
Also aliased as: has_key?
keys() click to toggle source

Returns an array populated with the keys of this object.

@return [Array]

# File lib/vagrant/registry.rb, line 42
def keys
  @items.keys
end
length() click to toggle source

Return the number of elements in this registry.

@return [Integer]

# File lib/vagrant/registry.rb, line 65
def length
  @items.keys.length
end
Also aliased as: size
map() { |key, get(key)| ... } click to toggle source

Iterate over the keyspace and return result

@return [Array]

# File lib/vagrant/registry.rb, line 56
def map(&block)
  @items.map do |key, _|
    yield key, get(key)
  end
end
merge(other) click to toggle source

Merge one registry with another and return a completely new registry. Note that the result cache is completely busted, so any gets on the new registry will result in a cache miss.

# File lib/vagrant/registry.rb, line 80
def merge(other)
  self.class.new.tap do |result|
    result.merge!(self)
    result.merge!(other)
  end
end
merge!(other) click to toggle source

Like #{merge} but merges into self.

# File lib/vagrant/registry.rb, line 88
def merge!(other)
  @items.merge!(other.__internal_state[:items])
  self
end
register(key, &block) click to toggle source

Register a key with a lazy-loaded value.

If a key with the given name already exists, it is overwritten.

# File lib/vagrant/registry.rb, line 15
def register(key, &block)
  raise ArgumentError, "block required" if !block_given?
  @items[key] = block
end
size()
Alias for: length
to_hash() click to toggle source

Converts this registry to a hash

# File lib/vagrant/registry.rb, line 94
def to_hash
  result = {}
  self.each do |key, value|
    result[key] = value
  end

  result
end