From 83bba789a447469767992ecd1dfb5e7961daf55a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 6 Feb 2013 15:21:34 -0800 Subject: [PATCH] Hook#apply --- lib/vagrant/action/hook.rb | 34 +++++++++++++++++++++++++++ test/unit/vagrant/action/hook_test.rb | 16 +++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/vagrant/action/hook.rb b/lib/vagrant/action/hook.rb index 0d89048e3..e7ac56cc8 100644 --- a/lib/vagrant/action/hook.rb +++ b/lib/vagrant/action/hook.rb @@ -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 diff --git a/test/unit/vagrant/action/hook_test.rb b/test/unit/vagrant/action/hook_test.rb index f944c3a54..27a22a271 100644 --- a/test/unit/vagrant/action/hook_test.rb +++ b/test/unit/vagrant/action/hook_test.rb @@ -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