Change method by which configuration classes register themselves to be cleaner

This commit is contained in:
Mitchell Hashimoto 2010-09-11 09:02:06 -07:00
parent b824a48569
commit 86465a36c0
12 changed files with 57 additions and 46 deletions

View File

@ -20,10 +20,6 @@ module Vagrant
config(env) config(env)
end end
def configures(key, klass)
config.class.configures(key, klass)
end
def config(env=nil) def config(env=nil)
@@config ||= Config::Top.new(env) @@config ||= Config::Top.new(env)
end end

View File

@ -6,6 +6,16 @@ module Vagrant
class Base class Base
attr_accessor :env attr_accessor :env
# Registers a subclass with the Vagrant configuration system so
# that it can then be used in Vagrantfiles.
#
# @param [Symbol] accessor The accessor on the main config object
# that is used to access the configuration class.
#
def self.configures(accessor, klass=self)
Top.configures(accessor, klass)
end
# Loads configuration values from JSON back into the proper # Loads configuration values from JSON back into the proper
# configuration classes. By default, this is done by simply # configuration classes. By default, this is done by simply
# iterating over all values in the JSON hash and assigning them # iterating over all values in the JSON hash and assigning them

View File

@ -1,7 +1,7 @@
module Vagrant module Vagrant
class Config class Config
class NFSConfig < Base class NFSConfig < Base
Config.configures :nfs, self configures :nfs
attr_accessor :map_uid attr_accessor :map_uid
attr_accessor :map_gid attr_accessor :map_gid

View File

@ -1,7 +1,7 @@
module Vagrant module Vagrant
class Config class Config
class PackageConfig < Base class PackageConfig < Base
Config.configures :package, self configures :package
attr_accessor :name attr_accessor :name
end end

View File

@ -1,7 +1,7 @@
module Vagrant module Vagrant
class Config class Config
class SSHConfig < Base class SSHConfig < Base
Config.configures :ssh, self configures :ssh
attr_accessor :username attr_accessor :username
attr_accessor :host attr_accessor :host

View File

@ -1,7 +1,7 @@
module Vagrant module Vagrant
class Config class Config
class VagrantConfig < Base class VagrantConfig < Base
Config.configures :vagrant, self configures :vagrant
attr_accessor :dotfile_name attr_accessor :dotfile_name
attr_accessor :log_output attr_accessor :log_output

View File

@ -1,7 +1,7 @@
module Vagrant module Vagrant
class Config class Config
class VMConfig < Base class VMConfig < Base
Config.configures :vm, self configures :vm
include Util::StackedProcRunner include Util::StackedProcRunner

View File

@ -62,6 +62,8 @@ module Vagrant
class Chef < Base class Chef < Base
# This is the configuration which is available through `config.chef` # This is the configuration which is available through `config.chef`
class ChefConfig < Vagrant::Config::Base class ChefConfig < Vagrant::Config::Base
configures :chef
# Chef server specific config # Chef server specific config
attr_accessor :chef_server_url attr_accessor :chef_server_url
attr_accessor :validation_key_path attr_accessor :validation_key_path
@ -124,9 +126,6 @@ module Vagrant
result result
end end
end end
# Tell the Vagrant configure class about our custom configuration
Config.configures :chef, ChefConfig
end end
end end
end end

View File

@ -13,7 +13,7 @@ module Vagrant
# generally, Vagrant tries to make almost every aspect of its execution # generally, Vagrant tries to make almost every aspect of its execution
# configurable, and this assists that goal. # configurable, and this assists that goal.
class LinuxConfig < Vagrant::Config::Base class LinuxConfig < Vagrant::Config::Base
Config.configures :linux, self configures :linux
attr_accessor :halt_timeout attr_accessor :halt_timeout
attr_accessor :halt_check_interval attr_accessor :halt_check_interval

View File

@ -9,7 +9,7 @@ module Vagrant
# generally, Vagrant tries to make almost every aspect of its execution # generally, Vagrant tries to make almost every aspect of its execution
# configurable, and this assists that goal. # configurable, and this assists that goal.
class SolarisConfig < Vagrant::Config::Base class SolarisConfig < Vagrant::Config::Base
Config.configures :solaris, self configures :solaris
attr_accessor :halt_timeout attr_accessor :halt_timeout
attr_accessor :halt_check_interval attr_accessor :halt_check_interval

View File

@ -2,36 +2,51 @@ require "test_helper"
class ConfigBaseTest < Test::Unit::TestCase class ConfigBaseTest < Test::Unit::TestCase
setup do setup do
@base = Vagrant::Config::Base.new @klass = Vagrant::Config::Base
end end
should "return a hash of instance variables" do context "class methods" do
data = { "foo" => "bar", "bar" => "baz" } should "enable configuration with proper accessor" do
klass = Class.new(@klass)
data.each do |iv, value| acc = :foo
@base.instance_variable_set("@#{iv}".to_sym, value) Vagrant::Config::Top.expects(:configures).with(acc, klass)
end klass.configures(acc)
result = @base.instance_variables_hash
assert_equal data.length, result.length
data.each do |iv, value|
assert_equal value, result[iv]
end end
end end
context "converting to JSON" do context "instance methods" do
should "include magic `json_class`" do setup do
@iv_hash = { "foo" => "bar" } @base = @klass.new
@base.expects(:instance_variables_hash).returns(@iv_hash)
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
assert_equal @json, @base.to_json
end end
should "not include env in the JSON hash" do should "return a hash of instance variables" do
@base.env = "FOO" data = { "foo" => "bar", "bar" => "baz" }
hash = @base.instance_variables_hash
assert !hash.has_key?(:env) data.each do |iv, value|
@base.instance_variable_set("@#{iv}".to_sym, value)
end
result = @base.instance_variables_hash
assert_equal data.length, result.length
data.each do |iv, value|
assert_equal value, result[iv]
end
end
context "converting to JSON" do
should "include magic `json_class`" do
@iv_hash = { "foo" => "bar" }
@base.expects(:instance_variables_hash).returns(@iv_hash)
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
assert_equal @json, @base.to_json
end
should "not include env in the JSON hash" do
@base.env = "FOO"
hash = @base.instance_variables_hash
assert !hash.has_key?(:env)
end
end end
end end
end end

View File

@ -72,15 +72,6 @@ class ConfigTest < Test::Unit::TestCase
end end
end end
context "adding configures" do
should "forward the method to the Top class" do
key = mock("key")
klass = mock("klass")
@klass::Top.expects(:configures).with(key, klass)
@klass.configures(key, klass)
end
end
context "resetting" do context "resetting" do
setup do setup do
@klass::Top.any_instance.stubs(:validate!) @klass::Top.any_instance.stubs(:validate!)