Class: Vagrant::Action::Hook
- Inherits:
-
Object
- Object
- Vagrant::Action::Hook
- Defined in:
- lib/vagrant/action/hook.rb
Overview
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.
Instance Attribute Summary collapse
-
#after_hooks ⇒ Hash<Class, Array<Class>>
readonly
This is a hash of the middleware to append to a certain other middleware.
-
#append_hooks ⇒ Array<Class>
readonly
This is a list of the hooks to just append to the end.
-
#before_hooks ⇒ Hash<Class, Array<Class>>
readonly
This is a hash of the middleware to prepend to a certain other middleware.
-
#prepend_hooks ⇒ Array<Class>
readonly
This is a list of the hooks to just prepend to the beginning.
Instance Method Summary collapse
-
#after(existing, new, *args, &block) ⇒ Object
Add a middleware after an existing middleware.
-
#append(new, *args, &block) ⇒ Object
Append a middleware to the end of the stack.
-
#apply(builder) ⇒ Object
This applies the given hook to a builder.
-
#before(existing, new, *args, &block) ⇒ Object
Add a middleware before an existing middleware.
-
#initialize ⇒ Hook
constructor
A new instance of Hook.
-
#prepend(new, *args, &block) ⇒ Object
Prepend a middleware to the beginning of the stack.
Constructor Details
#initialize ⇒ Hook
Returns a new instance of Hook.
29 30 31 32 33 34 |
# File 'lib/vagrant/action/hook.rb', line 29 def initialize @before_hooks = Hash.new { |h, k| h[k] = [] } @after_hooks = Hash.new { |h, k| h[k] = [] } @prepend_hooks = [] @append_hooks = [] end |
Instance Attribute Details
#after_hooks ⇒ Hash<Class, Array<Class>> (readonly)
This is a hash of the middleware to append to a certain other middleware.
17 18 19 |
# File 'lib/vagrant/action/hook.rb', line 17 def after_hooks @after_hooks end |
#append_hooks ⇒ Array<Class> (readonly)
This is a list of the hooks to just append to the end
27 28 29 |
# File 'lib/vagrant/action/hook.rb', line 27 def append_hooks @append_hooks end |
#before_hooks ⇒ Hash<Class, Array<Class>> (readonly)
This is a hash of the middleware to prepend to a certain other middleware.
11 12 13 |
# File 'lib/vagrant/action/hook.rb', line 11 def before_hooks @before_hooks end |
#prepend_hooks ⇒ Array<Class> (readonly)
This is a list of the hooks to just prepend to the beginning
22 23 24 |
# File 'lib/vagrant/action/hook.rb', line 22 def prepend_hooks @prepend_hooks end |
Instance Method Details
#after(existing, new, *args, &block) ⇒ Object
Add a middleware after an existing middleware.
48 49 50 |
# File 'lib/vagrant/action/hook.rb', line 48 def after(existing, new, *args, &block) @after_hooks[existing] << [new, args, block] end |
#append(new, *args, &block) ⇒ Object
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.
57 58 59 |
# File 'lib/vagrant/action/hook.rb', line 57 def append(new, *args, &block) @append_hooks << [new, args, block] end |
#apply(builder) ⇒ Object
This applies the given hook to a builder. This should not be called directly.
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 |
# File 'lib/vagrant/action/hook.rb', line 72 def apply(builder) # 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 # 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 |
#before(existing, new, *args, &block) ⇒ Object
Add a middleware before an existing middleware.
40 41 42 |
# File 'lib/vagrant/action/hook.rb', line 40 def before(existing, new, *args, &block) @before_hooks[existing] << [new, args, block] end |
#prepend(new, *args, &block) ⇒ Object
Prepend a middleware to the beginning of the stack.
64 65 66 |
# File 'lib/vagrant/action/hook.rb', line 64 def prepend(new, *args, &block) @prepend_hooks << [new, args, block] end |