Hooks allow parameters and blocks to middlewares

This commit is contained in:
Mitchell Hashimoto 2013-02-22 12:00:35 -08:00
parent a2cd7936ea
commit 18524628b7
2 changed files with 55 additions and 22 deletions

View File

@ -37,16 +37,16 @@ module Vagrant
#
# @param [Class] existing The existing middleware.
# @param [Class] new The new middleware.
def before(existing, new)
@before_hooks[existing] << new
def before(existing, new, *args, &block)
@before_hooks[existing] << [new, args, block]
end
# Add a middleware after an existing middleware.
#
# @param [Class] existing The existing middleware.
# @param [Class] new The new middleware.
def after(existing, new)
@after_hooks[existing] << new
def after(existing, new, *args, &block)
@after_hooks[existing] << [new, args, block]
end
# Append a middleware to the end of the stack. Note that if the
@ -54,15 +54,15 @@ module Vagrant
# be run.
#
# @param [Class] new The middleware to append.
def append(new)
@append_hooks << new
def append(new, *args, &block)
@append_hooks << [new, args, block]
end
# Prepend a middleware to the beginning of the stack.
#
# @param [Class] new The new middleware to prepend.
def prepend(new)
@prepend_hooks << new
def prepend(new, *args, &block)
@prepend_hooks << [new, args, block]
end
# This applies the given hook to a builder. This should not be
@ -71,21 +71,21 @@ module Vagrant
# @param [Builder] builder
def apply(builder)
# Prepends first
@prepend_hooks.each do |klass|
builder.insert(0, klass)
@prepend_hooks.each do |klass, args, block|
builder.insert(0, klass, *args, &block)
end
# Appends
@append_hooks.each do |klass|
builder.use(klass)
@append_hooks.each do |klass, args, block|
builder.use(klass, *args, &block)
end
# Before hooks
@before_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass|
builder.insert_before(key, klass)
list.each do |klass, args, block|
builder.insert_before(key, klass, *args, &block)
end
end
@ -93,8 +93,8 @@ module Vagrant
@after_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass|
builder.insert_after(key, klass)
list.each do |klass, args, block|
builder.insert_after(key, klass, *args, &block)
end
end
end

View File

@ -15,10 +15,17 @@ describe Vagrant::Action::Hook do
let(:existing) { "foo" }
it "should append them" do
block = Proc.new {}
subject.before(existing, 1)
subject.before(existing, 2)
subject.before(existing, 3, :arg, &block)
subject.before_hooks[existing].should == [1, 2]
subject.before_hooks[existing].should == [
[1, [], nil],
[2, [], nil],
[3, [:arg], block]
]
end
end
@ -26,28 +33,49 @@ describe Vagrant::Action::Hook do
let(:existing) { "foo" }
it "should append them" do
block = Proc.new {}
subject.after(existing, 1)
subject.after(existing, 2)
subject.after(existing, 3, :arg, &block)
subject.after_hooks[existing].should == [1, 2]
subject.after_hooks[existing].should == [
[1, [], nil],
[2, [], nil],
[3, [:arg], block]
]
end
end
describe "append" do
it "should make a list" do
block = Proc.new {}
subject.append(1)
subject.append(2)
subject.append(3, :arg, &block)
subject.append_hooks.should == [1, 2]
subject.append_hooks.should == [
[1, [], nil],
[2, [], nil],
[3, [:arg], block]
]
end
end
describe "prepend" do
it "should make a list" do
block = Proc.new {}
subject.prepend(1)
subject.prepend(2)
subject.prepend(3, :arg, &block)
subject.prepend_hooks.should == [1, 2]
subject.prepend_hooks.should == [
[1, [], nil],
[2, [], nil],
[3, [:arg], block]
]
end
end
@ -55,14 +83,19 @@ describe Vagrant::Action::Hook do
let(:builder) { Vagrant::Action::Builder.new }
it "should build the proper stack" do
subject.prepend("1")
subject.prepend("1", 2)
subject.append("9")
subject.after("1", "2")
subject.before("9", "8")
subject.apply(builder)
builder.stack.map(&:first).should == %w[1 2 8 9]
builder.stack.should == [
["1", [2], nil],
["2", [], nil],
["8", [], nil],
["9", [], nil]
]
end
end
end