From 80a7c8a0cba3d9f44f69f7a716166f101f0bef33 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 6 Feb 2013 14:21:31 -0800 Subject: [PATCH] Hook class --- lib/vagrant/action/hook.rb | 53 +++++++++++++++++++++++++++ lib/vagrant/action/warden.rb | 6 +-- test/unit/vagrant/action/hook_test.rb | 52 ++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 lib/vagrant/action/hook.rb create mode 100644 test/unit/vagrant/action/hook_test.rb diff --git a/lib/vagrant/action/hook.rb b/lib/vagrant/action/hook.rb new file mode 100644 index 000000000..ef8f2992d --- /dev/null +++ b/lib/vagrant/action/hook.rb @@ -0,0 +1,53 @@ +module Vagrant + module Action + # This class manages hooks into existing {Builder} stacks, and lets you + # add and remove middleware classes. This is the primary method by which + # plugins can hook into built-in middleware stacks. + class Hook + # This is a hash of the middleware to prepend to a certain + # other middleware. + # + # @return [Hash>] + attr_reader :before_hooks + + # This is a hash of the middleware to append to a certain other + # middleware. + # + # @return [Hash>] + attr_reader :after_hooks + + # This is a list of the hooks to just prepend to the beginning + # + # @return [Array] + attr_reader :prepend_hooks + + # This is a list of the hooks to just append to the end + # + # @return [Array] + attr_reader :append_hooks + + def initialize + @before_hooks = Hash.new { |h, k| h[k] = [] } + @after_hooks = Hash.new { |h, k| h[k] = [] } + @prepend_hooks = [] + @append_hooks = [] + end + + def before(existing, new) + @before_hooks[existing] << new + end + + def after(existing, new) + @after_hooks[existing] << new + end + + def append(new) + @append_hooks << new + end + + def prepend(new) + @prepend_hooks << new + end + end + end +end diff --git a/lib/vagrant/action/warden.rb b/lib/vagrant/action/warden.rb index 5629a13f2..d01b88caf 100644 --- a/lib/vagrant/action/warden.rb +++ b/lib/vagrant/action/warden.rb @@ -16,9 +16,9 @@ module Vagrant attr_accessor :actions, :stack def initialize(actions, env) - @stack = [] - @actions = actions.map { |m| finalize_action(m, env) } - @logger = Log4r::Logger.new("vagrant::action::warden") + @stack = [] + @actions = actions.map { |m| finalize_action(m, env) } + @logger = Log4r::Logger.new("vagrant::action::warden") @last_error = nil end diff --git a/test/unit/vagrant/action/hook_test.rb b/test/unit/vagrant/action/hook_test.rb new file mode 100644 index 000000000..f944c3a54 --- /dev/null +++ b/test/unit/vagrant/action/hook_test.rb @@ -0,0 +1,52 @@ +require File.expand_path("../../../base", __FILE__) + +require "vagrant/action/hook" + +describe Vagrant::Action::Hook do + describe "defaults" do + its("after_hooks") { should be_empty } + its("before_hooks") { should be_empty } + its("append_hooks") { should be_empty } + its("prepend_hooks") { should be_empty } + end + + describe "before hooks" do + let(:existing) { "foo" } + + it "should append them" do + subject.before(existing, 1) + subject.before(existing, 2) + + subject.before_hooks[existing].should == [1, 2] + end + end + + describe "after hooks" do + let(:existing) { "foo" } + + it "should append them" do + subject.after(existing, 1) + subject.after(existing, 2) + + subject.after_hooks[existing].should == [1, 2] + end + end + + describe "append" do + it "should make a list" do + subject.append(1) + subject.append(2) + + subject.append_hooks.should == [1, 2] + end + end + + describe "prepend" do + it "should make a list" do + subject.prepend(1) + subject.prepend(2) + + subject.prepend_hooks.should == [1, 2] + end + end +end