Plugin configuration can have scopes now, ex. provider

This commit is contained in:
Mitchell Hashimoto 2013-01-13 12:38:17 -08:00
parent 965427c540
commit e66c5066e4
7 changed files with 32 additions and 32 deletions

View File

@ -2,11 +2,18 @@ module Vagrant
module Plugin
module V2
# This is the container class for the components of a single plugin.
# This allows us to separate the plugin class which defines the
# components, and the actual container of those components. This
# removes a bit of state overhead from the plugin class itself.
class Components
attr_reader :provider_configs
# This contains all the configuration plugins by scope.
#
# @return [Hash<Symbol, Registry>]
attr_reader :configs
def initialize
@provider_configs = Registry.new
# Create the configs hash which defaults to a registry
@configs = Hash.new { |h, k| h[k] = Registry.new }
end
end
end

View File

@ -47,7 +47,7 @@ module Vagrant
result = {}
@registered.each do |plugin|
plugin.config.each do |key, klass|
plugin.components.configs[:top].each do |key, klass|
result[key] = klass
end
end
@ -101,7 +101,7 @@ module Vagrant
configs = {}
@registered.each do |plugin|
configs.merge!(plugin.components.provider_configs.to_hash)
configs.merge!(plugin.components.configs[:provider].to_hash)
end
configs

View File

@ -128,25 +128,10 @@ module Vagrant
#
# @param [String] name Configuration key.
# XXX: Document options hash
def self.config(name=UNSET_VALUE, options=nil, &block)
data[:config] ||= Registry.new
# Register a new config class only if a name was given.
if name != UNSET_VALUE
options ||= {}
if options[:provider]
# This config is for a specific provider. Register it as
# a provider config component.
components.provider_configs.register(name.to_sym, &block)
else
# This is a generic configuration plugin, register it as such.
data[:config].register(name.to_sym, &block)
end
end
# Return the registry
data[:config]
def self.config(name, scope=nil, &block)
scope ||= :top
components.configs[scope].register(name.to_sym, &block)
nil
end
# Defines an "easy hook," which gives an easier interface to hook

View File

@ -297,7 +297,7 @@ VF
p.provider(name) { provider_cls }
if config_class
p.config(name, :provider => name) { config_class }
p.config(name, :provider) { config_class }
end
end

View File

@ -1,9 +1,17 @@
require File.expand_path("../../../../base", __FILE__)
require "vagrant/registry"
describe Vagrant::Plugin::V2::Components do
let(:instance) { described_class.new }
it "should have provider configs" do
instance.provider_configs.should be_kind_of(Vagrant::Registry)
describe "configs" do
it "should have configs" do
instance.configs.should be_kind_of(Hash)
end
it "should default the values to registries" do
instance.configs[:i_probably_dont_exist].should be_kind_of(Vagrant::Registry)
end
end
end

View File

@ -98,11 +98,11 @@ describe Vagrant::Plugin::V2::Manager do
it "provides the collection of registered provider configs" do
pA = plugin do |p|
p.config("foo", :provider => true) { "foo" }
p.config("foo", :provider) { "foo" }
end
pB = plugin do |p|
p.config("bar", :provider => true) { "bar" }
p.config("bar", :provider) { "bar" }
p.config("baz") { "baz" }
end

View File

@ -106,7 +106,7 @@ describe Vagrant::Plugin::V2::Plugin do
config("foo") { "bar" }
end
plugin.config[:foo].should == "bar"
plugin.components.configs[:top][:foo].should == "bar"
end
it "should lazily register configuration classes" do
@ -123,16 +123,16 @@ describe Vagrant::Plugin::V2::Plugin do
# Now verify when we actually get the configuration key that
# a proper error is raised.
expect {
plugin.config[:foo]
plugin.components.configs[:top][:foo]
}.to raise_error(StandardError)
end
it "should register configuration classes for providers" do
plugin = Class.new(described_class) do
config("foo", :provider => true) { "bar" }
config("foo", :provider) { "bar" }
end
plugin.components.provider_configs[:foo].should == "bar"
plugin.components.configs[:provider][:foo].should == "bar"
end
end