Rename action registry to just Vagrant::Registry

This commit is contained in:
Mitchell Hashimoto 2011-12-11 18:22:06 -08:00
parent ad03163eff
commit e201d9cacf
7 changed files with 40 additions and 41 deletions

View File

@ -30,6 +30,7 @@ module Vagrant
autoload :Errors, 'vagrant/errors' autoload :Errors, 'vagrant/errors'
autoload :Hosts, 'vagrant/hosts' autoload :Hosts, 'vagrant/hosts'
autoload :Plugin, 'vagrant/plugin' autoload :Plugin, 'vagrant/plugin'
autoload :Registry, 'vagrant/registry'
autoload :SSH, 'vagrant/ssh' autoload :SSH, 'vagrant/ssh'
autoload :TestHelpers, 'vagrant/test_helpers' autoload :TestHelpers, 'vagrant/test_helpers'
autoload :UI, 'vagrant/ui' autoload :UI, 'vagrant/ui'

View File

@ -4,7 +4,6 @@ require 'vagrant/action/builtin'
module Vagrant module Vagrant
module Action module Action
autoload :Environment, 'vagrant/action/environment' autoload :Environment, 'vagrant/action/environment'
autoload :Registry, 'vagrant/action/registry'
autoload :Runner, 'vagrant/action/runner' autoload :Runner, 'vagrant/action/runner'
autoload :Warden, 'vagrant/action/warden' autoload :Warden, 'vagrant/action/warden'

View File

@ -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

View File

@ -200,13 +200,13 @@ module Vagrant
# Action registry for registering new actions with this environment. # Action registry for registering new actions with this environment.
# #
# @return [Action::Registry] # @return [Registry]
def action_registry def action_registry
return @action_registry if defined?(@action_registry) return @action_registry if defined?(@action_registry)
# The action registry hasn't been loaded yet, so load it # The action registry hasn't been loaded yet, so load it
# and setup the built-in actions with it. # and setup the built-in actions with it.
@action_registry = Action::Registry.new @action_registry = Registry.new
Vagrant::Action.builtin!(@action_registry) Vagrant::Action.builtin!(@action_registry)
@action_registry @action_registry
end end

31
lib/vagrant/registry.rb Normal file
View File

@ -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

View File

@ -78,7 +78,7 @@ describe Vagrant::Environment do
describe "action registry" do describe "action registry" do
it "has an 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 end
it "should have the built-in actions in the registry" do it "should have the built-in actions in the registry" do

View File

@ -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 } 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 instance.get("foo").should be_nil
end 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 expect do
instance.register("foo") do instance.register("foo") do
raise Exception, "BOOM!" raise Exception, "BOOM!"
@ -15,7 +15,7 @@ describe Vagrant::Action::Registry do
end.to_not raise_error end.to_not raise_error
end 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 object = Object.new
instance.register("foo") do instance.register("foo") do
object object