diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 0c0910f81..4bf7b152f 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -30,6 +30,7 @@ module Vagrant autoload :Errors, 'vagrant/errors' autoload :Hosts, 'vagrant/hosts' autoload :Plugin, 'vagrant/plugin' + autoload :Registry, 'vagrant/registry' autoload :SSH, 'vagrant/ssh' autoload :TestHelpers, 'vagrant/test_helpers' autoload :UI, 'vagrant/ui' diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb index 8d4bb2091..5433ffdc9 100644 --- a/lib/vagrant/action.rb +++ b/lib/vagrant/action.rb @@ -4,7 +4,6 @@ require 'vagrant/action/builtin' module Vagrant module Action autoload :Environment, 'vagrant/action/environment' - autoload :Registry, 'vagrant/action/registry' autoload :Runner, 'vagrant/action/runner' autoload :Warden, 'vagrant/action/warden' diff --git a/lib/vagrant/action/registry.rb b/lib/vagrant/action/registry.rb deleted file mode 100644 index 9213d2e9c..000000000 --- a/lib/vagrant/action/registry.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Vagrant - module Action - # This is the action registry, which stores action steps indexed - # by a unique name. These registry names can be used to call actions - # via the `Runner` class. - class Registry - def initialize - @actions = {} - end - - # Register a callable by key. - # - # The callable should be given in a block which will be lazily evaluated - # when the action is needed. - # - # If an action by the given name already exists then it will be - # overwritten. - def register(key, &block) - @actions[key] = block - end - - # Get an action by the given key. - # - # This will evaluate the block given to `register` and return the resulting - # action stack. - def get(key) - return nil if !@actions.has_key?(key) - @actions[key].call - end - end - end -end diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c3b61545b..13e565e12 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -200,13 +200,13 @@ module Vagrant # Action registry for registering new actions with this environment. # - # @return [Action::Registry] + # @return [Registry] def action_registry return @action_registry if defined?(@action_registry) # The action registry hasn't been loaded yet, so load it # and setup the built-in actions with it. - @action_registry = Action::Registry.new + @action_registry = Registry.new Vagrant::Action.builtin!(@action_registry) @action_registry end diff --git a/lib/vagrant/registry.rb b/lib/vagrant/registry.rb new file mode 100644 index 000000000..e343b3c7c --- /dev/null +++ b/lib/vagrant/registry.rb @@ -0,0 +1,31 @@ +module Vagrant + # Register components in a single location that can be queried. + # + # This allows certain components (such as guest systems, configuration + # pieces, etc.) to be registered and queried. + class Registry + def initialize + @actions = {} + end + + # Register a callable by key. + # + # The callable should be given in a block which will be lazily evaluated + # when the action is needed. + # + # If an action by the given name already exists then it will be + # overwritten. + def register(key, &block) + @actions[key] = block + end + + # Get an action by the given key. + # + # This will evaluate the block given to `register` and return the resulting + # action stack. + def get(key) + return nil if !@actions.has_key?(key) + @actions[key].call + end + end +end diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 536c15417..520500214 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -78,7 +78,7 @@ describe Vagrant::Environment do describe "action registry" do it "has an action registry" do - instance.action_registry.should be_kind_of(Vagrant::Action::Registry) + instance.action_registry.should be_kind_of(Vagrant::Registry) end it "should have the built-in actions in the registry" do diff --git a/test/unit/vagrant/action/registry_test.rb b/test/unit/vagrant/registry_test.rb similarity index 64% rename from test/unit/vagrant/action/registry_test.rb rename to test/unit/vagrant/registry_test.rb index eda383214..1ea69802c 100644 --- a/test/unit/vagrant/action/registry_test.rb +++ b/test/unit/vagrant/registry_test.rb @@ -1,13 +1,13 @@ -require File.expand_path("../../../base", __FILE__) +require File.expand_path("../../base", __FILE__) -describe Vagrant::Action::Registry do +describe Vagrant::Registry do let(:instance) { described_class.new } - it "should return nil for nonexistent actions" do + it "should return nil for nonexistent items" do instance.get("foo").should be_nil end - it "should register an action without calling the block yet" do + it "should register an item without calling the block yet" do expect do instance.register("foo") do raise Exception, "BOOM!" @@ -15,7 +15,7 @@ describe Vagrant::Action::Registry do end.to_not raise_error end - it "should call and return the result of a block when asking for the action" do + it "should call and return the result of a block when asking for the ite" do object = Object.new instance.register("foo") do object