Class: VagrantPlugins::ProxyConf::Action::OnlyOnce

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-proxyconf/action/only_once.rb

Overview

A middleware class that builds and runs the stack based on the specified block, but only once.

Instance Method Summary collapse

Constructor Details

#initialize(app, env, opts = {}, &block) ⇒ OnlyOnce

Returns a new instance of OnlyOnce.

Parameters:

  • opts (Hash) (defaults to: {})

    the options

Options Hash (opts):

  • :before (Boolean) — default: false

    should the block be called before (instead of after) passing control to the next middleware

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/vagrant-proxyconf/action/only_once.rb', line 12

def initialize(app, env, opts = {}, &block)
  raise ArgumentError, "A block must be given to OnlyOnce" if !block

  @app    = app
  @before = opts[:before]
  @block  = block
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vagrant-proxyconf/action/only_once.rb', line 20

def call(env)
  @app.call(env) if !@before

  if env[@block]
    logger.debug "Skipping repeated '#{@block}' stack"
  else
    logger.debug "'#{@block}' stack invoked first time"
    env[@block] = true

    new_env = build_and_run_block(env)
    env.merge!(new_env)
  end

  @app.call(env) if @before
end

#recover(env) ⇒ Object



36
37
38
39
# File 'lib/vagrant-proxyconf/action/only_once.rb', line 36

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