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)
end
def configures(key, klass)
config.class.configures(key, klass)
end
def config(env=nil)
@@config ||= Config::Top.new(env)
end

View File

@ -6,6 +6,16 @@ module Vagrant
class Base
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
# configuration classes. By default, this is done by simply
# iterating over all values in the JSON hash and assigning them

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,36 +2,51 @@ require "test_helper"
class ConfigBaseTest < Test::Unit::TestCase
setup do
@base = Vagrant::Config::Base.new
@klass = Vagrant::Config::Base
end
should "return a hash of instance variables" do
data = { "foo" => "bar", "bar" => "baz" }
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]
context "class methods" do
should "enable configuration with proper accessor" do
klass = Class.new(@klass)
acc = :foo
Vagrant::Config::Top.expects(:configures).with(acc, klass)
klass.configures(acc)
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
context "instance methods" do
setup do
@base = @klass.new
end
should "not include env in the JSON hash" do
@base.env = "FOO"
hash = @base.instance_variables_hash
assert !hash.has_key?(:env)
should "return a hash of instance variables" do
data = { "foo" => "bar", "bar" => "baz" }
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

View File

@ -72,15 +72,6 @@ class ConfigTest < Test::Unit::TestCase
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
setup do
@klass::Top.any_instance.stubs(:validate!)