Class: Vagrant::Config::V2::Root
- Inherits:
-
Object
- Object
- Vagrant::Config::V2::Root
- Defined in:
- lib/vagrant/config/v2/root.rb
Overview
This is the root configuration class. An instance of this is what is passed into version 2 Vagrant configuration blocks.
Instance Method Summary collapse
-
#__internal_state ⇒ Object
Returns the internal state of the root object.
-
#__set_internal_state(state) ⇒ Object
This sets the internal state.
-
#finalize! ⇒ Object
Called to finalize this object just prior to it being used by the Vagrant system.
-
#initialize(config_map, keys = nil) ⇒ Root
constructor
Initializes a root object that maps the given keys to specific configuration classes.
-
#method_missing(name, *args) ⇒ Object
We use method_missing as a way to get the configuration that is used for Vagrant and load the proper configuration classes for each.
-
#validate(machine, ignore_provider = nil) ⇒ Hash
This validates the configuration and returns a hash of error messages by section.
Constructor Details
#initialize(config_map, keys = nil) ⇒ Root
Initializes a root object that maps the given keys to specific configuration classes.
15 16 17 18 19 20 |
# File 'lib/vagrant/config/v2/root.rb', line 15 def initialize(config_map, keys=nil) @keys = keys || {} @config_map = config_map @missing_key_calls = Set.new @logger = Log4r::Logger.new("vagrant::config") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
We use method_missing as a way to get the configuration that is used for Vagrant and load the proper configuration classes for each.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vagrant/config/v2/root.rb', line 25 def method_missing(name, *args) return @keys[name] if @keys.key?(name) config_klass = @config_map[name.to_sym] if config_klass # Instantiate the class and return the instance @keys[name] = config_klass.new return @keys[name] else @logger.debug("missing key request name=#{name} loc=#{caller.first}") # Record access to a missing key as an error @missing_key_calls.add(name.to_s) return DummyConfig.new end end |
Instance Method Details
#__internal_state ⇒ Object
Returns the internal state of the root object. This is used by outside classes when merging, and shouldn't be called directly. Note the strange method name is to attempt to avoid any name clashes with potential configuration keys.
100 101 102 103 104 105 106 |
# File 'lib/vagrant/config/v2/root.rb', line 100 def __internal_state { "config_map" => @config_map, "keys" => @keys, "missing_key_calls" => @missing_key_calls } end |
#__set_internal_state(state) ⇒ Object
This sets the internal state. This is used by the core to do some merging logic and shouldn't be used by the general public.
110 111 112 113 114 |
# File 'lib/vagrant/config/v2/root.rb', line 110 def __set_internal_state(state) @config_map = state["config_map"] if state.key?("config_map") @keys = state["keys"] if state.key?("keys") @missing_key_calls = state["missing_key_calls"] if state.key?("missing_key_calls") end |
#finalize! ⇒ Object
Called to finalize this object just prior to it being used by the Vagrant system. The "!" signifies that this is expected to mutate itself.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/vagrant/config/v2/root.rb', line 44 def finalize! @config_map.each do |key, klass| if !@keys.key?(key) @keys[key] = klass.new end end @keys.each do |_key, instance| instance.finalize! instance._finalize! end end |
#validate(machine, ignore_provider = nil) ⇒ Hash
This validates the configuration and returns a hash of error messages by section. If there are no errors, an empty hash is returned.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant/config/v2/root.rb', line 63 def validate(machine, ignore_provider=nil) # Go through each of the configuration keys and validate errors = {} @keys.each do |_key, instance| if instance.respond_to?(:validate) # Validate this single item, and if we have errors then # we merge them into our total errors list. if _key == :vm result = instance.validate(machine, ignore_provider) else result = instance.validate(machine) end if result && !result.empty? errors = Util.merge_errors(errors, result) end end end # Go through and delete empty keys errors.keys.each do |key| errors.delete(key) if errors[key].empty? end # If we have missing keys, record those as errors if !@missing_key_calls.empty? errors["Vagrant"] = @missing_key_calls.to_a.sort.map do |key| I18n.t("vagrant.config.root.bad_key", key: key) end end errors end |