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

View File

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

View File

@ -40,26 +40,22 @@ class ActionBuilderTest < Test::Unit::TestCase
use 3 use 3
end 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 1
@instance.use other @instance.use other
assert_equal 2, @instance.stack.length assert_equal 3, @instance.stack.length
end end
end end
context "mergeable" do context "flatten" do
should "return the merge format with the builder" do should "return the flattened format of the builder" do
assert_equal [:merge, @instance], @instance.mergeable 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
end end