diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 71b4e5b48..f939bd0ec 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -18,7 +18,7 @@ module Vagrant # versions are available, mapped by the version string used in # `Vagrant.configure` calls. VERSIONS = Registry.new - VERSIONS.register("1") { V1 } + VERSIONS.register("1") { V1::Loader } # This is the order of versions. This is used by the loader to figure out # how to "upgrade" versions up to the desired (current) version. The diff --git a/lib/vagrant/config/v1.rb b/lib/vagrant/config/v1.rb index 71b54898b..bf8936a9d 100644 --- a/lib/vagrant/config/v1.rb +++ b/lib/vagrant/config/v1.rb @@ -1,103 +1,8 @@ -require "vagrant/config/version_base" -require "vagrant/config/v1/root" - module Vagrant module Config - # This is the "version 1" configuration loader. - class V1 < VersionBase - # Returns a bare empty configuration object. - # - # @return [V1::Root] - def self.init - new_root_object - end - - # Finalizes the configuration by making sure there is at least - # one VM defined in it. - def self.finalize(config) - # Call the `#finalize` method on each of the configuration keys. - # They're expected to modify themselves in our case. - config.finalize! - - # Return the object - config - end - - # Loads the configuration for the given proc and returns a configuration - # object. - # - # @param [Proc] config_proc - # @return [Object] - def self.load(config_proc) - # Create a root configuration object - root = new_root_object - - # Call the proc with the root - config_proc.call(root) - - # Return the root object, which doubles as the configuration object - # we actually use for accessing as well. - root - end - - # Merges two configuration objects. - # - # @param [V1::Root] old The older root config. - # @param [V1::Root] new The newer root config. - # @return [V1::Root] - def self.merge(old, new) - # Grab the internal states, we use these heavily throughout the process - old_state = old.__internal_state - new_state = new.__internal_state - - # The config map for the new object is the old one merged with the - # new one. - config_map = old_state["config_map"].merge(new_state["config_map"]) - - # Merge the keys. - old_keys = old_state["keys"] - new_keys = new_state["keys"] - keys = {} - old_keys.each do |key, old| - if new_keys.has_key?(key) - # We need to do a merge, which we expect to be available - # on the config class itself. - keys[key] = old.merge(new_keys[key]) - else - # We just take the old value, but dup it so that we can modify. - keys[key] = old.dup - end - end - - new_keys.each do |key, new| - # Add in the keys that the new class has that we haven't merged. - if !keys.has_key?(key) - keys[key] = new.dup - end - end - - # Return the final root object - V1::Root.new(config_map, keys) - end - - protected - - def self.new_root_object - # Get all the registered plugins - config_map = {} - Vagrant.plugin("1").registered.each do |plugin| - # Activate the plugin since we're about to use it - plugin.activate! - - # Get all the available configuration keys and add them to the map - plugin.config.each do |key, klass| - config_map[key] = klass - end - end - - # Create the configuration root object - V1::Root.new(config_map) - end + module V1 + autoload :Loader, "vagrant/config/v1/loader" + autoload :Root, "vagrant/config/v1/root" end end end diff --git a/lib/vagrant/config/v1/loader.rb b/lib/vagrant/config/v1/loader.rb new file mode 100644 index 000000000..d914c0ce6 --- /dev/null +++ b/lib/vagrant/config/v1/loader.rb @@ -0,0 +1,105 @@ +require "vagrant/config/v1/root" + +module Vagrant + module Config + module V1 + # This is the loader that handles configuration loading for V1 + # configurations. + class Loader < VersionBase + # Returns a bare empty configuration object. + # + # @return [V1::Root] + def self.init + new_root_object + end + + # Finalizes the configuration by making sure there is at least + # one VM defined in it. + def self.finalize(config) + # Call the `#finalize` method on each of the configuration keys. + # They're expected to modify themselves in our case. + config.finalize! + + # Return the object + config + end + + # Loads the configuration for the given proc and returns a configuration + # object. + # + # @param [Proc] config_proc + # @return [Object] + def self.load(config_proc) + # Create a root configuration object + root = new_root_object + + # Call the proc with the root + config_proc.call(root) + + # Return the root object, which doubles as the configuration object + # we actually use for accessing as well. + root + end + + # Merges two configuration objects. + # + # @param [V1::Root] old The older root config. + # @param [V1::Root] new The newer root config. + # @return [V1::Root] + def self.merge(old, new) + # Grab the internal states, we use these heavily throughout the process + old_state = old.__internal_state + new_state = new.__internal_state + + # The config map for the new object is the old one merged with the + # new one. + config_map = old_state["config_map"].merge(new_state["config_map"]) + + # Merge the keys. + old_keys = old_state["keys"] + new_keys = new_state["keys"] + keys = {} + old_keys.each do |key, old| + if new_keys.has_key?(key) + # We need to do a merge, which we expect to be available + # on the config class itself. + keys[key] = old.merge(new_keys[key]) + else + # We just take the old value, but dup it so that we can modify. + keys[key] = old.dup + end + end + + new_keys.each do |key, new| + # Add in the keys that the new class has that we haven't merged. + if !keys.has_key?(key) + keys[key] = new.dup + end + end + + # Return the final root object + V1::Root.new(config_map, keys) + end + + protected + + def self.new_root_object + # Get all the registered plugins + config_map = {} + Vagrant.plugin("1").registered.each do |plugin| + # Activate the plugin since we're about to use it + plugin.activate! + + # Get all the available configuration keys and add them to the map + plugin.config.each do |key, klass| + config_map[key] = klass + end + end + + # Create the configuration root object + V1::Root.new(config_map) + end + end + end + end +end diff --git a/lib/vagrant/config/v1/root.rb b/lib/vagrant/config/v1/root.rb index d82898a9e..7f561c431 100644 --- a/lib/vagrant/config/v1/root.rb +++ b/lib/vagrant/config/v1/root.rb @@ -1,6 +1,6 @@ module Vagrant module Config - class V1 < VersionBase + module V1 # This is the root configuration class. An instance of this is what # is passed into version 1 Vagrant configuration blocks. class Root diff --git a/test/unit/vagrant/config/v1_test.rb b/test/unit/vagrant/config/v1/loader_test.rb similarity index 97% rename from test/unit/vagrant/config/v1_test.rb rename to test/unit/vagrant/config/v1/loader_test.rb index 4e54ceb0e..96ce5c7af 100644 --- a/test/unit/vagrant/config/v1_test.rb +++ b/test/unit/vagrant/config/v1/loader_test.rb @@ -1,6 +1,6 @@ -require File.expand_path("../../../base", __FILE__) +require File.expand_path("../../../../base", __FILE__) -describe Vagrant::Config::V1 do +describe Vagrant::Config::V1::Loader do include_context "unit" describe "empty" do