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.
Direct Known Subclasses
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate over the keyspace.
-
#get(key) ⇒ Object
(also: #[])
Get an action by the given key.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#register(key, value = nil, &block) ⇒ Object
Register a callable by key.
-
#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 @actions = {} @results_cache = {} end |
Instance Method Details
#each(&block) ⇒ Object
Iterate over the keyspace.
36 37 38 39 40 |
# File 'lib/vagrant/registry.rb', line 36 def each(&block) @actions.each do |key, _| yield key, get(key) end end |
#get(key) ⇒ Object Also known as: []
Get an action by the given key.
This will evaluate the block given to register
and return the resulting
action stack.
28 29 30 31 32 |
# File 'lib/vagrant/registry.rb', line 28 def get(key) return nil if !@actions.has_key?(key) return @results_cache[key] if @results_cache.has_key?(key) @results_cache[key] = @actions[key].call end |
#register(key, value = nil, &block) ⇒ Object
Register a callable by key.
The callable should be given in a block which will be lazily evaluated when the action is needed.
If an action by the given name already exists then it will be overwritten.
19 20 21 22 |
# File 'lib/vagrant/registry.rb', line 19 def register(key, value=nil, &block) block = lambda { value } if value @actions[key] = block end |
#to_hash ⇒ Object
Converts this registry to a hash
43 44 45 46 47 48 49 50 |
# File 'lib/vagrant/registry.rb', line 43 def to_hash result = {} self.each do |key, value| result[key] = value end result end |