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 Plugin
module V2 module V2
# This is the container class for the components of a single plugin. # 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 class Components
attr_reader :provider_configs # This contains all the configuration plugins by scope.
#
# @return [Hash<Symbol, Registry>]
attr_reader :configs
def initialize 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 end
end end

View File

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

View File

@ -128,25 +128,10 @@ module Vagrant
# #
# @param [String] name Configuration key. # @param [String] name Configuration key.
# XXX: Document options hash # XXX: Document options hash
def self.config(name=UNSET_VALUE, options=nil, &block) def self.config(name, scope=nil, &block)
data[:config] ||= Registry.new scope ||= :top
components.configs[scope].register(name.to_sym, &block)
# Register a new config class only if a name was given. nil
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]
end end
# Defines an "easy hook," which gives an easier interface to hook # Defines an "easy hook," which gives an easier interface to hook

View File

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

View File

@ -1,9 +1,17 @@
require File.expand_path("../../../../base", __FILE__) require File.expand_path("../../../../base", __FILE__)
require "vagrant/registry"
describe Vagrant::Plugin::V2::Components do describe Vagrant::Plugin::V2::Components do
let(:instance) { described_class.new } let(:instance) { described_class.new }
it "should have provider configs" do describe "configs" do
instance.provider_configs.should be_kind_of(Vagrant::Registry) 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
end end

View File

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

View File

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