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

View File

@ -15,10 +15,17 @@ describe Vagrant::Action::Hook do
let(:existing) { "foo" } let(:existing) { "foo" }
it "should append them" do it "should append them" do
block = Proc.new {}
subject.before(existing, 1) subject.before(existing, 1)
subject.before(existing, 2) 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
end end
@ -26,28 +33,49 @@ describe Vagrant::Action::Hook do
let(:existing) { "foo" } let(:existing) { "foo" }
it "should append them" do it "should append them" do
block = Proc.new {}
subject.after(existing, 1) subject.after(existing, 1)
subject.after(existing, 2) 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
end end
describe "append" do describe "append" do
it "should make a list" do it "should make a list" do
block = Proc.new {}
subject.append(1) subject.append(1)
subject.append(2) 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
end end
describe "prepend" do describe "prepend" do
it "should make a list" do it "should make a list" do
block = Proc.new {}
subject.prepend(1) subject.prepend(1)
subject.prepend(2) 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
end end
@ -55,14 +83,19 @@ describe Vagrant::Action::Hook do
let(:builder) { Vagrant::Action::Builder.new } let(:builder) { Vagrant::Action::Builder.new }
it "should build the proper stack" do it "should build the proper stack" do
subject.prepend("1") subject.prepend("1", 2)
subject.append("9") subject.append("9")
subject.after("1", "2") subject.after("1", "2")
subject.before("9", "8") subject.before("9", "8")
subject.apply(builder) 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 end
end end