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.



10
11
12
13
# File 'lib/vagrant/registry.rb', line 10

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

Instance Method Details

#__internal_stateObject



106
107
108
109
110
111
# File 'lib/vagrant/registry.rb', line 106

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

#each(&block) ⇒ Object

Iterate over the keyspace.



50
51
52
53
54
# File 'lib/vagrant/registry.rb', line 50

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

#empty?Boolean

Checks if this registry has any items.

Returns:

  • (Boolean)


76
77
78
# File 'lib/vagrant/registry.rb', line 76

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.



27
28
29
30
31
# File 'lib/vagrant/registry.rb', line 27

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)


37
38
39
# File 'lib/vagrant/registry.rb', line 37

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

#keysArray

Returns an array populated with the keys of this object.

Returns:

  • (Array)


45
46
47
# File 'lib/vagrant/registry.rb', line 45

def keys
  @items.keys
end

#lengthInteger Also known as: size

Return the number of elements in this registry.

Returns:

  • (Integer)


68
69
70
# File 'lib/vagrant/registry.rb', line 68

def length
  @items.keys.length
end

#map(&block) ⇒ Array

Iterate over the keyspace and return result

Returns:

  • (Array)


59
60
61
62
63
# File 'lib/vagrant/registry.rb', line 59

def map(&block)
  @items.map do |key, _|
    yield key, get(key)
  end
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.



83
84
85
86
87
88
# File 'lib/vagrant/registry.rb', line 83

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.



91
92
93
94
# File 'lib/vagrant/registry.rb', line 91

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)


18
19
20
21
# File 'lib/vagrant/registry.rb', line 18

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

#to_hashObject

Converts this registry to a hash



97
98
99
100
101
102
103
104
# File 'lib/vagrant/registry.rb', line 97

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

  result
end