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<Builder::StackItem>> (readonly)

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

Returns:


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

def after_hooks
  @after_hooks
end

#append_hooksArray<Builder::StackItem> (readonly)

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

Returns:


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

def append_hooks
  @append_hooks
end

#before_hooksHash<Class, Array<Builder::StackItem>> (readonly)

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

Returns:


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

def before_hooks
  @before_hooks
end

#prepend_hooksArray<Builder::StackItem> (readonly)

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

Returns:


22
23
24
# File 'lib/vagrant/action/hook.rb', line 22

def prepend_hooks
  @prepend_hooks
end

Instance Method Details

#after(existing, new, *args, **keywords, &block) ⇒ Object

Add a middleware after an existing middleware.

Parameters:

  • existing (Class)

    The existing middleware.

  • new (Class)

    The new middleware.


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vagrant/action/hook.rb', line 56

def after(existing, new, *args, **keywords, &block)
  item = Builder::StackItem.new(
    middleware: new,
    arguments: Builder::MiddlewareArguments.new(
      parameters: args,
      keywords: keywords,
      block: block
    )
  )
  @after_hooks[existing] << item
end

#append(new, *args, **keywords, &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.


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vagrant/action/hook.rb', line 73

def append(new, *args, **keywords, &block)
  item = Builder::StackItem.new(
    middleware: new,
    arguments: Builder::MiddlewareArguments.new(
      parameters: args,
      keywords: keywords,
      block: block
    )
  )
  @append_hooks << item
end

#apply(builder, options = {}) ⇒ Object

This applies the given hook to a builder. This should not be called directly.

Parameters:


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vagrant/action/hook.rb', line 112

def apply(builder, options={})
  if !options[:no_prepend_or_append]
    # Prepends first
    @prepend_hooks.each do |item|
      if options[:root]
        idx = builder.index(options[:root])
      else
        idx = 0
      end
      builder.insert(idx, item.middleware, *item.arguments.parameters,
        **item.arguments.keywords, &item.arguments.block)
    end

    # Appends
    @append_hooks.each do |item|
      if options[:root]
        idx = builder.index(options[:root])
        builder.insert(idx + 1, item.middleware, *item.arguments.parameters,
          **item.arguments.keywords, &item.arguments.block)
      else
        builder.use(item.middleware, *item.arguments.parameters,
          **item.arguments.keywords, &item.arguments.block)
      end
    end
  end

  # Before hooks
  @before_hooks.each do |key, list|
    next if !builder.index(key)

    list.each do |item|
      builder.insert_before(key, item.middleware, *item.arguments.parameters,
        **item.arguments.keywords, &item.arguments.block)
    end
  end

  # After hooks
  @after_hooks.each do |key, list|
    next if !builder.index(key)

    list.each do |item|
      builder.insert_after(key, item.middleware, *item.arguments.parameters,
        **item.arguments.keywords, &item.arguments.block)
    end
  end
end

#before(existing, new, *args, **keywords, &block) ⇒ Object

Add a middleware before an existing middleware.

Parameters:

  • existing (Class)

    The existing middleware.

  • new (Class)

    The new middleware.


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant/action/hook.rb', line 40

def before(existing, new, *args, **keywords, &block)
  item = Builder::StackItem.new(
    middleware: new,
    arguments: Builder::MiddlewareArguments.new(
      parameters: args,
      keywords: keywords,
      block: block
    )
  )
  @before_hooks[existing] << item
end

#empty?Boolean

Returns:

  • (Boolean)

101
102
103
104
105
106
# File 'lib/vagrant/action/hook.rb', line 101

def empty?
  before_hooks.empty? &&
    after_hooks.empty? &&
    prepend_hooks.empty? &&
    append_hooks.empty?
end

#prepend(new, *args, **keywords, &block) ⇒ Object

Prepend a middleware to the beginning of the stack.

Parameters:

  • new (Class)

    The new middleware to prepend.


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant/action/hook.rb', line 88

def prepend(new, *args, **keywords, &block)
  item = Builder::StackItem.new(
    middleware: new,
    arguments: Builder::MiddlewareArguments.new(
      parameters: args,
      keywords: keywords,
      block: block
    )
  )
  @prepend_hooks << item
end