Hook class

This commit is contained in:
Mitchell Hashimoto 2013-02-06 14:21:31 -08:00
parent 8f24d2d98c
commit 80a7c8a0cb
3 changed files with 108 additions and 3 deletions

View File

@ -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<Class, Array<Class>>]
attr_reader :before_hooks
# This is a hash of the middleware to append to a certain other
# middleware.
#
# @return [Hash<Class, Array<Class>>]
attr_reader :after_hooks
# This is a list of the hooks to just prepend to the beginning
#
# @return [Array<Class>]
attr_reader :prepend_hooks
# This is a list of the hooks to just append to the end
#
# @return [Array<Class>]
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

View File

@ -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

View File

@ -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