Registered actions can be run. Builders can use other builders

This commit is contained in:
Mitchell Hashimoto 2010-07-04 04:09:02 +02:00
parent bad5ba559f
commit 5775292408
4 changed files with 32 additions and 1 deletions

View File

@ -31,9 +31,12 @@ module Vagrant
end end
# Runs the given callable object in the context of the environment. # Runs the given callable object in the context of the environment.
# If a symbol is given as the `callable` parameter, then it is looked
# up in the registered actions list which are registered with {register}.
# #
# @param [Object] callable An object which responds to `call`. # @param [Object] callable An object which responds to `call`.
def run(callable) def run(callable)
callable = self.class.actions[callable] if callable.kind_of?(Symbol)
callable.call(Action::Environment.new(env)) callable.call(Action::Environment.new(env))
end end
end end

View File

@ -37,7 +37,12 @@ module Vagrant
# #
# @param [Class] middleware The middleware class # @param [Class] middleware The middleware class
def use(middleware, *args, &block) def use(middleware, *args, &block)
stack << [middleware, args, block] if middleware.kind_of?(Builder)
# Merge in the other builder's stack into our own
self.stack.concat(middleware.stack)
else
self.stack << [middleware, args, block]
end
end end
# Converts the builder stack to a runnable action sequence. # Converts the builder stack to a runnable action sequence.

View File

@ -33,6 +33,17 @@ class ActionBuilderTest < Test::Unit::TestCase
@instance.use 2 @instance.use 2
assert_equal [2, [], nil], @instance.stack.last assert_equal [2, [], nil], @instance.stack.last
end end
should "merge in other builder's stack" do
other = @klass.new do
use 2
use 3
end
@instance.use 1
@instance.use other
assert_equal 3, @instance.stack.length
end
end end
context "converting to an app" do context "converting to an app" do

View File

@ -10,6 +10,10 @@ class ActionTest < Test::Unit::TestCase
@instance = @klass.new(mock_environment) @instance = @klass.new(mock_environment)
end end
teardown do
@klass.actions.clear
end
should "run the callable item with the proper context" do should "run the callable item with the proper context" do
callable = mock("callable") callable = mock("callable")
callable.expects(:call).with() do |env| callable.expects(:call).with() do |env|
@ -20,5 +24,13 @@ class ActionTest < Test::Unit::TestCase
@instance.run(callable) @instance.run(callable)
end end
should "run the registered callable if a symbol is given" do
callable = mock("callable")
callable.expects(:call).once
@klass.register(:call, callable)
@instance.run(:call)
end
end end
end end