Class: Vagrant::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/registry.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.


7
8
9
10
# File 'lib/vagrant/registry.rb', line 7

def initialize
  @items = {}
  @results_cache = {}
end

Instance Method Details

#__internal_stateObject


94
95
96
97
98
99
# File 'lib/vagrant/registry.rb', line 94

def __internal_state
  {
    items: @items,
    results_cache: @results_cache
  }
end

#each(&block) ⇒ Object

Iterate over the keyspace.


47
48
49
50
51
# File 'lib/vagrant/registry.rb', line 47

def each(&block)
  @items.each do |key, _|
    yield key, get(key)
  end
end

#empty?Boolean

Checks if this registry has any items.

Returns:

  • (Boolean)

64
65
66
# File 'lib/vagrant/registry.rb', line 64

def empty?
  @items.keys.empty?
end

#get(key) ⇒ Object Also known as: []

Get a value by the given key.

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


24
25
26
27
28
# 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

#key?(key) ⇒ Boolean Also known as: has_key?

Checks if the given key is registered with the registry.

Returns:

  • (Boolean)

34
35
36
# File 'lib/vagrant/registry.rb', line 34

def key?(key)
  @items.key?(key)
end

#keysArray

Returns an array populated with the keys of this object.

Returns:

  • (Array)

42
43
44
# File 'lib/vagrant/registry.rb', line 42

def keys
  @items.keys
end

#lengthInteger Also known as: size

Return the number of elements in this registry.

Returns:

  • (Integer)

56
57
58
# File 'lib/vagrant/registry.rb', line 56

def length
  @items.keys.length
end

#merge(other) ⇒ Object

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.


71
72
73
74
75
76
# File 'lib/vagrant/registry.rb', line 71

def merge(other)
  self.class.new.tap do |result|
    result.merge!(self)
    result.merge!(other)
  end
end

#merge!(other) ⇒ Object

Like ##merge but merges into self.


79
80
81
82
# File 'lib/vagrant/registry.rb', line 79

def merge!(other)
  @items.merge!(other.__internal_state[:items])
  self
end

#register(key, &block) ⇒ Object

Register a key with a lazy-loaded value.

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

Raises:

  • (ArgumentError)

15
16
17
18
# File 'lib/vagrant/registry.rb', line 15

def register(key, &block)
  raise ArgumentError, "block required" if !block_given?
  @items[key] = block
end

#to_hashObject

Converts this registry to a hash


85
86
87
88
89
90
91
92
# File 'lib/vagrant/registry.rb', line 85

def to_hash
  result = {}
  self.each do |key, value|
    result[key] = value
  end

  result
end