-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathhook.rb
More file actions
107 lines (92 loc) · 3.12 KB
/
hook.rb
File metadata and controls
107 lines (92 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
# Add a middleware before an existing middleware.
#
# @param [Class] existing The existing middleware.
# @param [Class] new The new middleware.
def before(existing, new, *args, &block)
@before_hooks[existing] << [new, args, block]
end
# Add a middleware after an existing middleware.
#
# @param [Class] existing The existing middleware.
# @param [Class] new The new middleware.
def after(existing, new, *args, &block)
@after_hooks[existing] << [new, args, block]
end
# Append a middleware to the end of the stack. Note that if the
# middleware sequence ends early, then the new middleware won't
# be run.
#
# @param [Class] new The middleware to append.
def append(new, *args, &block)
@append_hooks << [new, args, block]
end
# Prepend a middleware to the beginning of the stack.
#
# @param [Class] new The new middleware to prepend.
def prepend(new, *args, &block)
@prepend_hooks << [new, args, block]
end
# This applies the given hook to a builder. This should not be
# called directly.
#
# @param [Builder] builder
def apply(builder, options=nil)
options ||= {}
if !options[:no_prepend_or_append]
# Prepends first
@prepend_hooks.each do |klass, args, block|
builder.insert(0, klass, *args, &block)
end
# Appends
@append_hooks.each do |klass, args, block|
builder.use(klass, *args, &block)
end
end
# Before hooks
@before_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass, args, block|
builder.insert_before(key, klass, *args, &block)
end
end
# After hooks
@after_hooks.each do |key, list|
next if !builder.index(key)
list.each do |klass, args, block|
builder.insert_after(key, klass, *args, &block)
end
end
end
end
end
end