Make mergeable default behaviour and make flatten the explicit behavior

This commit is contained in:
Mitchell Hashimoto 2010-07-06 20:50:35 -07:00
parent 642db533ee
commit 68f1e6c7e1
3 changed files with 21 additions and 23 deletions

View File

@ -34,8 +34,10 @@ module Vagrant
# Returns a mergeable version of the builder. If `use` is called with
# the return value of this method, then the stack will merge, instead
# of being treated as a separate single middleware.
def mergeable
[:merge, self]
def flatten
lambda do |env|
self.call(env)
end
end
# Adds a middleware class to the middleware stack. Any additional
@ -44,9 +46,9 @@ module Vagrant
#
# @param [Class] middleware The middleware class
def use(middleware, *args, &block)
if middleware.kind_of?(Array) && middleware[0] == :merge
if middleware.kind_of?(Builder)
# Merge in the other builder's stack into our own
self.stack.concat(middleware[1].stack)
self.stack.concat(middleware.stack)
else
self.stack << [middleware, args, block]
end

View File

@ -28,8 +28,8 @@ module Vagrant
# reload - Halts then restarts the VM
reload = Builder.new do
use Action[:halt].mergeable
use Action[:start].mergeable
use Action[:halt]
use Action[:start]
end
register :reload, reload
@ -40,14 +40,14 @@ module Vagrant
use VM::Persist
use VM::MatchMACAddress
use VM::CheckGuestAdditions
use Action[:start].mergeable
use Action[:start]
end
register :up, up
# destroy - Halts, cleans up, and destroys an existing VM
destroy = Builder.new do
use Action[:halt].mergeable
use Action[:halt]
use VM::DestroyUnusedNetworkInterfaces
use VM::Destroy
end

View File

@ -40,26 +40,22 @@ class ActionBuilderTest < Test::Unit::TestCase
use 3
end
@instance.use 1
@instance.use other.mergeable
assert_equal 3, @instance.stack.length
end
should "not merge in another builder if not mergeable" do
other = @klass.new do
use 2
use 3
end
@instance.use 1
@instance.use other
assert_equal 2, @instance.stack.length
assert_equal 3, @instance.stack.length
end
end
context "mergeable" do
should "return the merge format with the builder" do
assert_equal [:merge, @instance], @instance.mergeable
context "flatten" do
should "return the flattened format of the builder" do
env = Vagrant::Action::Environment.new(nil)
env.expects(:foo).once
func = lambda { |x| x.foo }
@instance.use func
proc = @instance.flatten
assert proc.respond_to?(:call)
proc.call(env)
end
end