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.
-
#get(key) ⇒ Object
(also: #[])
Get a value by the given key.
-
#has_key?(key) ⇒ Boolean
Checks if the given key is registered with the registry.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#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.
7 8 9 10 |
# File 'lib/vagrant/registry.rb', line 7 def initialize @items = {} @results_cache = {} end |
Instance Method Details
#__internal_state ⇒ Object
71 72 73 74 75 76 |
# File 'lib/vagrant/registry.rb', line 71 def __internal_state { :items => @items, :results_cache => @results_cache } end |
#each(&block) ⇒ Object
Iterate over the keyspace.
39 40 41 42 43 |
# File 'lib/vagrant/registry.rb', line 39 def each(&block) @items.each do |key, _| yield key, get(key) end 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.has_key?(key) return @results_cache[key] if @results_cache.has_key?(key) @results_cache[key] = @items[key].call end |
#has_key?(key) ⇒ Boolean
Checks if the given key is registered with the registry.
34 35 36 |
# File 'lib/vagrant/registry.rb', line 34 def has_key?(key) @items.has_key?(key) 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.
48 49 50 51 52 53 |
# File 'lib/vagrant/registry.rb', line 48 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.
56 57 58 59 |
# File 'lib/vagrant/registry.rb', line 56 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.
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_hash ⇒ Object
Converts this registry to a hash
62 63 64 65 66 67 68 69 |
# File 'lib/vagrant/registry.rb', line 62 def to_hash result = {} self.each do |key, value| result[key] = value end result end |