Class: Vagrant::Action::Builtin::Confirm

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

Overview

This class asks the user to confirm some sort of question with a "Y/N" question. The only parameter is the text to ask the user. The result is placed in env[:result] so that it can be used with the Call class.

Direct Known Subclasses

DestroyConfirm

Instance Method Summary collapse

Constructor Details

#initialize(app, env, message, force_key = nil, **opts) ⇒ Confirm

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

Parameters:

  • message (String)

    The message to ask the user.

  • force_key (Symbol) (defaults to: nil)

    The key that if present and true in the environment hash will skip the confirmation question.


14
15
16
17
18
19
# File 'lib/vagrant/action/builtin/confirm.rb', line 14

def initialize(app, env, message, force_key=nil, **opts)
  @app      = app
  @message  = message
  @force_key = force_key
  @allowed  = opts[:allowed]
end

Instance Method Details

#call(env) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant/action/builtin/confirm.rb', line 21

def call(env)
  choice = nil

  # If we have a force key set and we're forcing, then set
  # the result to "Y"
  choice = "Y" if @force_key && env[@force_key]

  if !choice
    while true
      # If we haven't chosen yes, then ask the user via TTY
      choice = env[:ui].ask(@message)

      # If we don't have an allowed set just exit
      break if !@allowed
      break if @allowed.include?(choice)
    end
  end

  # The result is only true if the user said "Y"
  env[:result] = choice && choice.upcase == "Y"
  env["#{@force_key}_result".to_sym] = env[:result]

  @app.call(env)
end