Class: Vagrant::Action::Hook

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeHook

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_hooksHash<Class, Array<Class>> (readonly)

This is a hash of the middleware to append to a certain other middleware.

Returns:

  • (Hash<Class, Array<Class>>)


17
18
19
# File 'lib/vagrant/action/hook.rb', line 17

def after_hooks
  @after_hooks
end

#append_hooksArray<Class> (readonly)

This is a list of the hooks to just append to the end

Returns:

  • (Array<Class>)


27
28
29
# File 'lib/vagrant/action/hook.rb', line 27

def append_hooks
  @append_hooks
end

#before_hooksHash<Class, Array<Class>> (readonly)

This is a hash of the middleware to prepend to a certain other middleware.

Returns:

  • (Hash<Class, Array<Class>>)


11
12
13
# File 'lib/vagrant/action/hook.rb', line 11

def before_hooks
  @before_hooks
end

#prepend_hooksArray<Class> (readonly)

This is a list of the hooks to just prepend to the beginning

Returns:

  • (Array<Class>)


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.

Parameters:

  • existing (Class)

    The existing middleware.

  • new (Class)

    The new 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.

Parameters:

  • new (Class)

    The middleware to append.



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.

Parameters:



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.

Parameters:

  • existing (Class)

    The existing middleware.

  • new (Class)

    The new 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.

Parameters:

  • new (Class)

    The new middleware to prepend.



64
65
66
# File 'lib/vagrant/action/hook.rb', line 64

def prepend(new, *args, &block)
  @prepend_hooks << [new, args, block]
end