Hook#apply

This commit is contained in:
Mitchell Hashimoto 2013-02-06 15:21:34 -08:00
parent d720205810
commit 83bba789a4
2 changed files with 50 additions and 0 deletions

View File

@ -64,6 +64,40 @@ module Vagrant
def prepend(new)
@prepend_hooks << new
end
# This applies the given hook to a builder. This should not be
# called directly.
#
# @param [Builder] builder
def apply(builder)
# Prepends first
@prepend_hooks.each do |klass|
builder.insert(0, klass)
end
# Appends
@append_hooks.each do |klass|
builder.use(klass)
end
# Before hooks
@before_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass|
builder.insert_before(key, klass)
end
end
# After hooks
@after_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass|
builder.insert_after(key, klass)
end
end
end
end
end
end

View File

@ -1,5 +1,6 @@
require File.expand_path("../../../base", __FILE__)
require "vagrant/action/builder"
require "vagrant/action/hook"
describe Vagrant::Action::Hook do
@ -49,4 +50,19 @@ describe Vagrant::Action::Hook do
subject.prepend_hooks.should == [1, 2]
end
end
describe "applying" do
let(:builder) { Vagrant::Action::Builder.new }
it "should build the proper stack" do
subject.prepend("1")
subject.append("9")
subject.after("1", "2")
subject.before("9", "8")
subject.apply(builder)
builder.stack.map(&:first).should == %w[1 2 8 9]
end
end
end