diff --git a/lib/vagrant/config/base.rb b/lib/vagrant/config/base.rb deleted file mode 100644 index f3b95f960..000000000 --- a/lib/vagrant/config/base.rb +++ /dev/null @@ -1,82 +0,0 @@ -module Vagrant - module Config - # The base class for all configuration classes. This implements - # basic things such as the environment instance variable which all - # config classes need as well as a basic `to_json` implementation. - class Base - # Loads configuration values from JSON back into the proper - # configuration classes. By default, this is done by simply - # iterating over all values in the JSON hash and assigning them - # to instance variables on the class. - def self.json_create(data) - data.inject(new) do |result, data| - key, value = data - result.instance_variable_set("@#{key}".to_sym, value) if key != "json_class" - result - end - end - - # Allows setting options from a hash. By default this simply calls - # the `#{key}=` method on the config class with the value, which is - # the expected behavior most of the time. - def set_options(options) - options.each do |key, value| - send("#{key}=", value) - end - end - - # Merge another configuration object into this one. - # - # @param [Object] other The other configuration object to merge from, - # this must be the same type of object as this one. - # @return [Object] The merged object. - def merge(other) - result = self.class.new - instance_variables_hash.merge(other.instance_variables_hash).each do |key, value| - # Ignore keys that start with a double underscore. This allows - # configuration classes to still hold around internal state - # that isn't propagated. - if !key.start_with?("__") - result.instance_variable_set("@#{key}".to_sym, value) - end - end - - result - end - - # Called by {Top} after the configuration is loaded to validate - # the configuaration objects. Subclasses should implement this - # method and add any errors to the `errors` object given. - # - # @param [ErrorRecorder] errors - def validate(env, errors); end - - # Converts the configuration to a raw hash by calling `#to_hash` - # on all instance variables (if it can) and putting them into - # a hash. - def to_hash - instance_variables_hash.inject({}) do |acc, data| - k,v = data - v = v.to_hash if v.respond_to?(:to_hash) - acc[k] = v - acc - end - end - - # Converts to JSON, with the `json_class` field set so that when - # the JSON is parsed back, it can be loaded back into the proper class. - # See {json_create}. - def to_json(*a) - instance_variables_hash.to_json(*a) - end - - # Returns the instance variables as a hash of key-value pairs. - def instance_variables_hash - instance_variables.inject({}) do |acc, iv| - acc[iv.to_s[1..-1]] = instance_variable_get(iv) - acc - end - end - end - end -end diff --git a/lib/vagrant/config/v1.rb b/lib/vagrant/config/v1.rb index 1eb4a775e..71b54898b 100644 --- a/lib/vagrant/config/v1.rb +++ b/lib/vagrant/config/v1.rb @@ -1,5 +1,4 @@ require "vagrant/config/version_base" -require "vagrant/config/v1/base" require "vagrant/config/v1/root" module Vagrant diff --git a/lib/vagrant/config/v1/base.rb b/lib/vagrant/config/v1/base.rb deleted file mode 100644 index 349f0e973..000000000 --- a/lib/vagrant/config/v1/base.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Vagrant - module Config - class V1 < VersionBase - # Base class for configuration keys. It is not required to inherit - # from this class but this class provides useful helpers that config - # classes may wish to use. - class Base - # This is called as a last-minute hook that allows the configuration - # object to finalize itself before it will be put into use. This is - # a useful place to do some defaults in the case the user didn't - # configure something or so on. - # - # The configuration object is expected to mutate itself. - def finalize! - end - - # Merge another configuration object into this one. This assumes that - # the other object is the same class as this one. - # - # @param [Object] other The other configuration object to merge from, - # this must be the same type of object as this one. - # @return [Object] The merged object. - def merge(other) - result = self.class.new - - # Set all of our instance variables on the new class - [self, other].each do |obj| - obj.instance_variables.each do |key| - # Ignore keys that start with a double underscore. This allows - # configuration classes to still hold around internal state - # that isn't propagated. - if !key.to_s.start_with?("__") - result.instance_variable_set(key, obj.instance_variable_get(key)) - end - end - end - - result - end - - # Called by {Root} after the configuration is loaded to validate - # the configuaration objects. Subclasses should implement this - # method and add any errors to the `errors` object given. - # - # @param [ErrorRecorder] errors - def validate(env, errors); end - end - end - end -end diff --git a/lib/vagrant/plugin/v1.rb b/lib/vagrant/plugin/v1.rb index b6de22963..0e21f7500 100644 --- a/lib/vagrant/plugin/v1.rb +++ b/lib/vagrant/plugin/v1.rb @@ -5,6 +5,7 @@ require "vagrant/plugin/v1/errors" module Vagrant module Plugin module V1 + autoload :Config, "vagrant/plugin/v1/config" autoload :Plugin, "vagrant/plugin/v1/plugin" end end diff --git a/lib/vagrant/plugin/v1/config.rb b/lib/vagrant/plugin/v1/config.rb new file mode 100644 index 000000000..9a5fa68bf --- /dev/null +++ b/lib/vagrant/plugin/v1/config.rb @@ -0,0 +1,94 @@ +module Vagrant + module Plugin + module V1 + # This is the base class for a configuration key defined for + # V1. Any configuration key plugins for V1 should inherit from this + # class. + class Config + # This is called as a last-minute hook that allows the configuration + # object to finalize itself before it will be put into use. This is + # a useful place to do some defaults in the case the user didn't + # configure something or so on. + # + # An example of where this sort of thing is used or has been used: + # the "vm" configuration key uses this to make sure that at least + # one sub-VM has been defined: the default VM. + # + # The configuration object is expected to mutate itself. + def finalize! + # Default implementation is to do nothing. + end + + # Merge another configuration object into this one. This assumes that + # the other object is the same class as this one. This should not + # mutate this object, but instead should return a new, merged object. + # + # The default implementation will simply iterate over the instance + # variables and merge them together, with this object overriding + # any conflicting instance variables of the older object. Instance + # variables starting with "__" (double underscores) will be ignored. + # This lets you set some sort of instance-specific state on your + # configuration keys without them being merged together later. + # + # @param [Object] other The other configuration object to merge from, + # this must be the same type of object as this one. + # @return [Object] The merged object. + def merge(other) + result = self.class.new + + # Set all of our instance variables on the new class + [self, other].each do |obj| + obj.instance_variables.each do |key| + # Ignore keys that start with a double underscore. This allows + # configuration classes to still hold around internal state + # that isn't propagated. + if !key.to_s.start_with?("@__") + result.instance_variable_set(key, obj.instance_variable_get(key)) + end + end + end + + result + end + + # Allows setting options from a hash. By default this simply calls + # the `#{key}=` method on the config class with the value, which is + # the expected behavior most of the time. + # + # This is expected to mutate itself. + # + # @param [Hash] options A hash of options to set on this configuration + # key. + def set_options(options) + options.each do |key, value| + send("#{key}=", value) + end + end + + # Converts this configuration object to JSON. + def to_json(*a) + instance_variables_hash.to_json(*a) + end + + # Returns the instance variables as a hash of key-value pairs. + def instance_variables_hash + instance_variables.inject({}) do |acc, iv| + acc[iv.to_s[1..-1]] = instance_variable_get(iv) + acc + end + end + + # Called after the configuration is finalized and loaded to validate + # this object. + # + # @param [Environment] env Vagrant::Environment object of the + # environment that this configuration has been loaded into. This + # gives you convenient access to things like the the root path + # and so on. + # @param [ErrorRecorder] errors + def validate(env, errors) + end + end + end + end +end diff --git a/plugins/guests/freebsd/config.rb b/plugins/guests/freebsd/config.rb index d544bf7b6..0ad4cbccb 100644 --- a/plugins/guests/freebsd/config.rb +++ b/plugins/guests/freebsd/config.rb @@ -1,6 +1,6 @@ module VagrantPlugins module GuestFreeBSD - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :halt_timeout attr_accessor :halt_check_interval diff --git a/plugins/guests/linux/config.rb b/plugins/guests/linux/config.rb index 06e765ffb..87e27739c 100644 --- a/plugins/guests/linux/config.rb +++ b/plugins/guests/linux/config.rb @@ -1,6 +1,6 @@ module VagrantPlugins module GuestLinux - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :halt_timeout attr_accessor :halt_check_interval diff --git a/plugins/guests/solaris/config.rb b/plugins/guests/solaris/config.rb index e26c8205d..b47afc6db 100644 --- a/plugins/guests/solaris/config.rb +++ b/plugins/guests/solaris/config.rb @@ -1,6 +1,6 @@ module VagrantPlugins module GuestSolaris - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :halt_timeout attr_accessor :halt_check_interval # This sets the command to use to execute items as a superuser. sudo is default diff --git a/plugins/kernel_v1/config/nfs.rb b/plugins/kernel_v1/config/nfs.rb index 8df6f01d4..8d8546b83 100644 --- a/plugins/kernel_v1/config/nfs.rb +++ b/plugins/kernel_v1/config/nfs.rb @@ -2,7 +2,7 @@ require "vagrant" module VagrantPlugins module Kernel_V1 - class NFSConfig < Vagrant::Config::V1::Base + class NFSConfig < Vagrant::Plugin::V1::Config attr_accessor :map_uid attr_accessor :map_gid end diff --git a/plugins/kernel_v1/config/package.rb b/plugins/kernel_v1/config/package.rb index 12d5a7e64..93b1f62c0 100644 --- a/plugins/kernel_v1/config/package.rb +++ b/plugins/kernel_v1/config/package.rb @@ -2,7 +2,7 @@ require "vagrant" module VagrantPlugins module Kernel_V1 - class PackageConfig < Vagrant::Config::V1::Base + class PackageConfig < Vagrant::Plugin::V1::Config attr_accessor :name end end diff --git a/plugins/kernel_v1/config/ssh.rb b/plugins/kernel_v1/config/ssh.rb index 2302865d9..5cf6f20e2 100644 --- a/plugins/kernel_v1/config/ssh.rb +++ b/plugins/kernel_v1/config/ssh.rb @@ -2,7 +2,7 @@ require "vagrant" module VagrantPlugins module Kernel_V1 - class SSHConfig < Vagrant::Config::V1::Base + class SSHConfig < Vagrant::Plugin::V1::Config attr_accessor :username attr_accessor :password attr_accessor :host diff --git a/plugins/kernel_v1/config/vagrant.rb b/plugins/kernel_v1/config/vagrant.rb index 6f6500161..e74e69e49 100644 --- a/plugins/kernel_v1/config/vagrant.rb +++ b/plugins/kernel_v1/config/vagrant.rb @@ -2,7 +2,7 @@ require "vagrant" module VagrantPlugins module Kernel_V1 - class VagrantConfig < Vagrant::Config::V1::Base + class VagrantConfig < Vagrant::Plugin::V1::Config attr_accessor :dotfile_name attr_accessor :host end diff --git a/plugins/kernel_v1/config/vm.rb b/plugins/kernel_v1/config/vm.rb index 10b6dfe32..7bb6d49ad 100644 --- a/plugins/kernel_v1/config/vm.rb +++ b/plugins/kernel_v1/config/vm.rb @@ -7,7 +7,7 @@ require File.expand_path("../vm_subvm", __FILE__) module VagrantPlugins module Kernel_V1 - class VMConfig < Vagrant::Config::V1::Base + class VMConfig < Vagrant::Plugin::V1::Config DEFAULT_VM_NAME = :default attr_accessor :name diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index 1f558b72b..3b88aeb58 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -83,7 +83,7 @@ module VagrantPlugins end # This is the configuration which is available through `config.chef` - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config # Shared config attr_accessor :node_name attr_accessor :provisioning_path diff --git a/plugins/provisioners/puppet/provisioner/puppet.rb b/plugins/provisioners/puppet/provisioner/puppet.rb index e036bb71d..30d7cf12c 100644 --- a/plugins/provisioners/puppet/provisioner/puppet.rb +++ b/plugins/provisioners/puppet/provisioner/puppet.rb @@ -8,7 +8,7 @@ module VagrantPlugins end class Puppet < Vagrant::Provisioners::Base - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :manifest_file attr_accessor :manifests_path attr_accessor :module_path @@ -120,7 +120,7 @@ module VagrantPlugins def set_module_paths @module_paths = [] @expanded_module_paths.each_with_index do |path, i| - @module_paths << [path, File.join(config.pp_path, "modules-#{i}"] + @module_paths << [path, File.join(config.pp_path, "modules-#{i}")] end end diff --git a/plugins/provisioners/puppet/provisioner/puppet_server.rb b/plugins/provisioners/puppet/provisioner/puppet_server.rb index 11d74b94d..537a3d94f 100644 --- a/plugins/provisioners/puppet/provisioner/puppet_server.rb +++ b/plugins/provisioners/puppet/provisioner/puppet_server.rb @@ -6,7 +6,7 @@ module VagrantPlugins end class PuppetServer < Base - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :puppet_server attr_accessor :puppet_node attr_accessor :options diff --git a/plugins/provisioners/shell/provisioner.rb b/plugins/provisioners/shell/provisioner.rb index 6811361de..1f7ef866d 100644 --- a/plugins/provisioners/shell/provisioner.rb +++ b/plugins/provisioners/shell/provisioner.rb @@ -4,7 +4,7 @@ require "tempfile" module VagrantPlugins module Shell class Provisioner < Vagrant::Provisioners::Base - class Config < Vagrant::Config::Base + class Config < Vagrant::Plugin::V1::Config attr_accessor :inline attr_accessor :path attr_accessor :upload_path diff --git a/test/unit/vagrant/config/base_test.rb b/test/unit/vagrant/config/base_test.rb deleted file mode 100644 index 2a0a5266a..000000000 --- a/test/unit/vagrant/config/base_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require File.expand_path("../../../base", __FILE__) - -describe Vagrant::Config::Base do - include_context "unit" - - let(:foo_class) do - Class.new(described_class) do - attr_accessor :one - attr_accessor :two - end - end - - it "should merge by default by simply copying each instance variable" do - one = foo_class.new - one.one = 2 - one.two = 1 - - two = foo_class.new - two.two = 5 - - result = one.merge(two) - result.one.should == 2 - result.two.should == 5 - end - - it "doesn't merge values that start with a double underscore" do - bar_class = Class.new(foo_class) do - class_variable_set(:@@counter, 0) - - def initialize - @__test = self.class.send(:class_variable_get, :@@counter) - self.class.send(:class_variable_set, :@@counter, @__test + 1) - end - end - - one = bar_class.new - one.one = 2 - one.two = 1 - - two = bar_class.new - two.two = 5 - - # Verify the counters - one.instance_variable_get(:@__test).should == 0 - two.instance_variable_get(:@__test).should == 1 - one.merge(two).instance_variable_get(:@__test).should == 2 - end -end diff --git a/test/unit/vagrant/plugin/v1/config_test.rb b/test/unit/vagrant/plugin/v1/config_test.rb new file mode 100644 index 000000000..32e08c518 --- /dev/null +++ b/test/unit/vagrant/plugin/v1/config_test.rb @@ -0,0 +1,44 @@ +require File.expand_path("../../../../base", __FILE__) + +describe Vagrant::Plugin::V1::Config do + include_context "unit" + + let(:foo_class) do + Class.new(described_class) do + attr_accessor :one + attr_accessor :two + end + end + + describe "merging" do + it "should merge by default by simply copying each instance variable" do + one = foo_class.new + one.one = 2 + one.two = 1 + + two = foo_class.new + two.two = 5 + + result = one.merge(two) + result.one.should == 2 + result.two.should == 5 + end + + it "doesn't merge values that start with a double underscore" do + one = foo_class.new + one.one = 1 + one.two = 1 + one.instance_variable_set(:@__bar, "one") + + two = foo_class.new + two.two = 2 + two.instance_variable_set(:@__bar, "two") + + # Merge and verify + result = one.merge(two) + result.one.should == 1 + result.two.should == 2 + result.instance_variable_get(:@__bar).should be_nil + end + end +end