Class: Vagrant::Action::Builtin::Call

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

Overview

This middleware class allows a sort of “conditional” run within a single middlware sequence. It takes another middleware runnable, runs it with the same environment, then yields the resulting env to a block, allowing that block to determine the next course of action in the middleware sequence.

The first argument to this middleware sequence is anywhere middleware runnable, whether it be a class, lambda, or something else that responds to ‘call`. This middleware runnable is run with the same environment as this class.

After running, Call takes the environment and yields it to a block given to initialize the class, along with an instance of Vagrant::Action::Builder. The result is used to build up a new sequence on the given builder. This builder is then run.

Instance Method Summary collapse

Constructor Details

#initialize(app, env, callable, *callable_args) {|result, builder| ... } ⇒ Call

For documentation, read the description of the Vagrant::Action::Builtin::Call class.

Parameters:

  • callable (Object)

    A valid middleware runnable object. This can be a class, a lambda, or an object that responds to ‘call`.

Yields:

  • (result, builder)

    This block is expected to build on ‘builder` which is the next middleware sequence that will be run.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
# File 'lib/vagrant/action/builtin/call.rb', line 26

def initialize(app, env, callable, *callable_args, &block)
  raise ArgumentError, "A block must be given to Call" if !block

  @app      = app
  @callable = callable
  @callable_args = callable_args
  @block    = block
  @child_app = nil
end

Instance Method Details

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant/action/builtin/call.rb', line 36

def call(env)
  runner  = Runner.new

  # Build the callable that we'll run
  callable = Builder.build(@callable, *@callable_args)

  # Run our callable with our environment
  new_env = runner.run(callable, env)

  # Build our new builder based on the result
  builder = Builder.new
  @block.call(new_env, builder)

  # Run the result with our new environment
  @child_app = builder.to_app(new_env)
  final_env = runner.run(@child_app, new_env)

  # Merge the environment into our original environment
  env.merge!(final_env)

  # Call the next step using our final environment
  @app.call(env)
end

#recover(env) ⇒ Object



60
61
62
63
# File 'lib/vagrant/action/builtin/call.rb', line 60

def recover(env)
  # Call back into our compiled application and recover it.
  @child_app.recover(env) if @child_app
end