Config class now allows classes to dynamically register configuration keys.
This commit is contained in:
parent
3daf48b70c
commit
19d00a8802
|
@ -13,6 +13,10 @@ module Vagrant
|
||||||
config_runners.clear
|
config_runners.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def configures(key, klass)
|
||||||
|
@@config.class.configures(key, klass)
|
||||||
|
end
|
||||||
|
|
||||||
def config
|
def config
|
||||||
@@config ||= Config::Top.new
|
@@config ||= Config::Top.new
|
||||||
end
|
end
|
||||||
|
@ -129,18 +133,30 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
class Top < Base
|
class Top < Base
|
||||||
attr_reader :package
|
@@configures = []
|
||||||
attr_reader :ssh
|
|
||||||
attr_reader :vm
|
class <<self
|
||||||
attr_reader :chef
|
def configures_list
|
||||||
attr_reader :vagrant
|
@@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
|
def initialize
|
||||||
@ssh = SSHConfig.new
|
self.class.configures_list.each do |key, klass|
|
||||||
@vm = VMConfig.new
|
instance_variable_set("@#{key}".to_sym, klass.new)
|
||||||
@chef = ChefConfig.new
|
end
|
||||||
@vagrant = VagrantConfig.new
|
|
||||||
@package = PackageConfig.new
|
|
||||||
|
|
||||||
@loaded = false
|
@loaded = false
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||||
|
|
||||||
class ConfigTest < Test::Unit::TestCase
|
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
|
context "resetting" do
|
||||||
setup do
|
setup do
|
||||||
Vagrant::Config.run { |config| }
|
Vagrant::Config.run { |config| }
|
||||||
|
@ -119,4 +128,53 @@ class ConfigTest < Test::Unit::TestCase
|
||||||
assert !result.has_key?("json")
|
assert !result.has_key?("json")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue