Class: Vagrant::Registry
- Inherits:
-
Object
- Object
- Vagrant::Registry
- 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
- #__internal_state ⇒ Object
-
#each(&block) ⇒ Object
Iterate over the keyspace.
-
#empty? ⇒ Boolean
Checks if this registry has any items.
-
#get(key) ⇒ Object
(also: #[])
Get a value by the given key.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Checks if the given key is registered with the registry.
-
#keys ⇒ Array
Returns an array populated with the keys of this object.
-
#length ⇒ Integer
(also: #size)
Return the number of elements in this registry.
-
#map(&block) ⇒ Array
Iterate over the keyspace and return result.
-
#merge(other) ⇒ Object
Merge one registry with another and return a completely new registry.
-
#merge!(other) ⇒ Object
Like ##merge but merges into self.
-
#register(key, &block) ⇒ Object
Register a key with a lazy-loaded value.
-
#to_hash ⇒ Object
Converts this registry to a hash.
Constructor Details
#initialize ⇒ Registry
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_state ⇒ Object
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.
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.
37 38 39 |
# File 'lib/vagrant/registry.rb', line 37 def key?(key) @items.key?(key) end |
#keys ⇒ Array
Returns an array populated with the keys of this object.
45 46 47 |
# File 'lib/vagrant/registry.rb', line 45 def keys @items.keys end |
#length ⇒ Integer Also known as: size
Return the number of elements in this registry.
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
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.
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_hash ⇒ Object
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 |