Class: Vagrant::Action::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/builder.rb

Overview

Action builder which provides a nice DSL for building up a middleware sequence for Vagrant actions. This code is based heavily off of Rack::Builder and ActionDispatch::MiddlewareStack in Rack and Rails, respectively.

Usage

Building an action sequence is very easy:

app = Vagrant::Action::Builder.new do
  use MiddlewareA
  use MiddlewareB
end

Vagrant::Action.run(app)

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Initializes the builder. An optional block can be passed which will be evaluated in the context of the instance.



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

def initialize(&block)
  instance_eval(&block) if block_given?
end

Instance Method Details

#call(env) ⇒ Object

Runs the builder stack with the given environment.



91
92
93
# File 'lib/vagrant/action/builder.rb', line 91

def call(env)
  to_app(env).call(env)
end

#delete(index) ⇒ Object

Deletes the given middleware object or index



85
86
87
88
# File 'lib/vagrant/action/builder.rb', line 85

def delete(index)
  index = self.index(index) unless index.is_a?(Integer)
  stack.delete_at(index)
end

#flattenObject

Returns a mergeable version of the builder. If use is called with the return value of this method, then the stack will merge, instead of being treated as a separate single middleware.



29
30
31
32
33
# File 'lib/vagrant/action/builder.rb', line 29

def flatten
  lambda do |env|
    self.call(env)
  end
end

#insert(index, middleware, *args, &block) ⇒ Object Also known as: insert_before

Inserts a middleware at the given index or directly before the given middleware object.



58
59
60
61
# File 'lib/vagrant/action/builder.rb', line 58

def insert(index, middleware, *args, &block)
  index = self.index(index) unless index.is_a?(Integer)
  stack.insert(index, [middleware, args, block])
end

#insert_after(index, middleware, *args, &block) ⇒ Object

Inserts a middleware after the given index or middleware object.



66
67
68
69
70
# File 'lib/vagrant/action/builder.rb', line 66

def insert_after(index, middleware, *args, &block)
  index = self.index(index) unless index.is_a?(Integer)
  raise "no such middleware to insert after: #{index.inspect}" unless index
  insert(index + 1, middleware, *args, &block)
end

#replace(index, middleware, *args, &block) ⇒ Object

Replaces the given middlware object or index with the new middleware.



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

def replace(index, middleware, *args, &block)
  if index.is_a?(Integer)
    delete(index)
    insert(index, middleware, *args, &block)
  else
    insert_before(index, middleware, *args, &block)
    delete(index)
  end
end

#use(middleware, *args, &block) ⇒ Object

Adds a middleware class to the middleware stack. Any additional args and a block, if given, are saved and passed to the initializer of the middleware.

Parameters:

  • middleware (Class)

    The middleware class



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant/action/builder.rb', line 40

def use(middleware, *args, &block)
  # Prepend with a environment setter if args are given
  if !args.empty? && args.first.is_a?(Hash) && middleware != Env::Set
    self.use(Env::Set, args.shift, &block)
  end

  if middleware.kind_of?(Builder)
    # Merge in the other builder's stack into our own
    self.stack.concat(middleware.stack)
  else
    self.stack << [middleware, args, block]
  end

  self
end