From e66c5066e4b25f55fc57fcf2b03ed4324a4036d7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 13 Jan 2013 12:38:17 -0800 Subject: [PATCH] Plugin configuration can have scopes now, ex. provider --- lib/vagrant/plugin/v2/components.rb | 11 +++++++-- lib/vagrant/plugin/v2/manager.rb | 4 ++-- lib/vagrant/plugin/v2/plugin.rb | 23 ++++--------------- test/unit/vagrant/environment_test.rb | 2 +- .../unit/vagrant/plugin/v2/components_test.rb | 12 ++++++++-- test/unit/vagrant/plugin/v2/manager_test.rb | 4 ++-- test/unit/vagrant/plugin/v2/plugin_test.rb | 8 +++---- 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/vagrant/plugin/v2/components.rb b/lib/vagrant/plugin/v2/components.rb index b982b8c0a..2301656da 100644 --- a/lib/vagrant/plugin/v2/components.rb +++ b/lib/vagrant/plugin/v2/components.rb @@ -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] + 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 diff --git a/lib/vagrant/plugin/v2/manager.rb b/lib/vagrant/plugin/v2/manager.rb index 18ec2e013..8fdfa6097 100644 --- a/lib/vagrant/plugin/v2/manager.rb +++ b/lib/vagrant/plugin/v2/manager.rb @@ -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 diff --git a/lib/vagrant/plugin/v2/plugin.rb b/lib/vagrant/plugin/v2/plugin.rb index 28a27a026..611835626 100644 --- a/lib/vagrant/plugin/v2/plugin.rb +++ b/lib/vagrant/plugin/v2/plugin.rb @@ -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 diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index de6a66623..7361c9039 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v2/components_test.rb b/test/unit/vagrant/plugin/v2/components_test.rb index 30c2df403..bf98ca71b 100644 --- a/test/unit/vagrant/plugin/v2/components_test.rb +++ b/test/unit/vagrant/plugin/v2/components_test.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v2/manager_test.rb b/test/unit/vagrant/plugin/v2/manager_test.rb index 46d044ece..c2d3f811b 100644 --- a/test/unit/vagrant/plugin/v2/manager_test.rb +++ b/test/unit/vagrant/plugin/v2/manager_test.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v2/plugin_test.rb b/test/unit/vagrant/plugin/v2/plugin_test.rb index d6acb0128..6248f0c9c 100644 --- a/test/unit/vagrant/plugin/v2/plugin_test.rb +++ b/test/unit/vagrant/plugin/v2/plugin_test.rb @@ -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