Class: TTY::Prompt::ConfirmQuestion

Inherits:
Question
  • Object
show all
Defined in:
lib/tty/prompt/confirm_question.rb

Constant Summary

Constants inherited from Question

Question::UndefinedSetting

Instance Attribute Summary

Attributes inherited from Question

#message, #messages, #modifier, #validation

Instance Method Summary collapse

Methods inherited from Question

#convert, #convert?, #convert_result, #default, #default?, #echo, #in, #in?, #inspect, #message_for, #modify, #quiet, #raw, #read_input, #refresh, #render, #render_error, #required, #to_s, #validate, #validation?, #value, #value?

Constructor Details

#initialize(prompt, **options) ⇒ ConfirmQuestion

Create confirmation question

Options Hash (**options):

  • :suffix (String)
  • :positive (String)
  • :negative (String)


17
18
19
20
21
22
# File 'lib/tty/prompt/confirm_question.rb', line 17

def initialize(prompt, **options)
  super
  @suffix   = options.fetch(:suffix)   { UndefinedSetting }
  @positive = options.fetch(:positive) { UndefinedSetting }
  @negative = options.fetch(:negative) { UndefinedSetting }
end

Instance Method Details

#call(message, &block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/tty/prompt/confirm_question.rb', line 63

def call(message, &block)
  return if Utils.blank?(message)

  @message = message
  block.call(self) if block
  setup_defaults
  render
end

#negative(value = (not_set = true)) ⇒ Object

Set value for matching negative choice



57
58
59
60
61
# File 'lib/tty/prompt/confirm_question.rb', line 57

def negative(value = (not_set = true))
  return @negative if not_set

  @negative = value
end

#negative?Boolean



28
29
30
# File 'lib/tty/prompt/confirm_question.rb', line 28

def negative?
  @negative != UndefinedSetting
end

#positive(value = (not_set = true)) ⇒ Object

Set value for matching positive choice



48
49
50
51
52
# File 'lib/tty/prompt/confirm_question.rb', line 48

def positive(value = (not_set = true))
  return @positive if not_set

  @positive = value
end

#positive?Boolean



24
25
26
# File 'lib/tty/prompt/confirm_question.rb', line 24

def positive?
  @positive != UndefinedSetting
end

#render_questionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render confirmation question



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tty/prompt/confirm_question.rb', line 77

def render_question
  header = "#{@prefix}#{message} "
  if !@done
    header += @prompt.decorate("(#{@suffix})", @help_color) + " "
  else
    answer = conversion.call(@input)
    label  = answer ? @positive : @negative
    header += @prompt.decorate(label, @active_color)
  end
  header << "\n" if @done
  header
end

#suffix(value = (not_set = true)) ⇒ Object

Set question suffix



39
40
41
42
43
# File 'lib/tty/prompt/confirm_question.rb', line 39

def suffix(value = (not_set = true))
  return @negative if not_set

  @suffix = value
end

#suffix?Boolean



32
33
34
# File 'lib/tty/prompt/confirm_question.rb', line 32

def suffix?
  @suffix != UndefinedSetting
end