Config class now allows classes to dynamically register configuration keys.

This commit is contained in:
Mitchell Hashimoto 2010-03-10 12:25:53 -08:00
parent 3daf48b70c
commit 19d00a8802
2 changed files with 84 additions and 10 deletions

View File

@ -13,6 +13,10 @@ module Vagrant
config_runners.clear
end
def configures(key, klass)
@@config.class.configures(key, klass)
end
def config
@@config ||= Config::Top.new
end
@ -129,18 +133,30 @@ module Vagrant
end
class Top < Base
attr_reader :package
attr_reader :ssh
attr_reader :vm
attr_reader :chef
attr_reader :vagrant
@@configures = []
class <<self
def configures_list
@@configures ||= []
end
def configures(key, klass)
configures_list << [key, klass]
attr_reader key.to_sym
end
end
# Setup default configures
configures :package, PackageConfig
configures :ssh, SSHConfig
configures :vm, VMConfig
configures :chef, ChefConfig
configures :vagrant, VagrantConfig
def initialize
@ssh = SSHConfig.new
@vm = VMConfig.new
@chef = ChefConfig.new
@vagrant = VagrantConfig.new
@package = PackageConfig.new
self.class.configures_list.each do |key, klass|
instance_variable_set("@#{key}".to_sym, klass.new)
end
@loaded = false
end

View File

@ -1,6 +1,15 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ConfigTest < Test::Unit::TestCase
context "adding configures" do
should "forward the method to the Top class" do
key = mock("key")
klass = mock("klass")
Vagrant::Config::Top.expects(:configures).with(key, klass)
Vagrant::Config.configures(key, klass)
end
end
context "resetting" do
setup do
Vagrant::Config.run { |config| }
@ -119,4 +128,53 @@ class ConfigTest < Test::Unit::TestCase
assert !result.has_key?("json")
end
end
context "top config class" do
setup do
@configures_list = []
Vagrant::Config::Top.stubs(:configures_list).returns(@configures_list)
Vagrant::Config::Top.stubs(:attr_reader)
end
context "adding configure keys" do
setup do
@key = "top_config_foo"
@klass = mock("klass")
end
should "add key and klass to configures list" do
@configures_list.expects(:<<).with([@key, @klass])
Vagrant::Config::Top.configures(@key, @klass)
end
end
context "initializing" do
should "initialize each configurer and set it to its key" do
5.times do |i|
key = "key#{i}"
klass = mock("klass#{i}")
instance = mock("instance#{i}")
klass.expects(:new).returns(instance)
@configures_list << [key, klass]
end
Vagrant::Config::Top.new
end
end
context "loaded status" do
setup do
@top= Vagrant::Config::Top.new
end
should "not be loaded by default" do
assert !@top.loaded?
end
should "be loaded after calling loaded!" do
@top.loaded!
assert @top.loaded?
end
end
end
end