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, #process_input, #raw, #read_input, #refresh, #render, #render_error, #required, #to_s, #validate, #validation?

Constructor Details

#initialize(prompt, options = {}) ⇒ ConfirmQuestion

Create confirmation question

Parameters:

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

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



60
61
62
63
64
65
66
# File 'lib/tty/prompt/confirm_question.rb', line 60

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



55
56
57
58
# File 'lib/tty/prompt/confirm_question.rb', line 55

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

#negative?Boolean

Returns:

  • (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



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

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

#positive?Boolean

Returns:

  • (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

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/tty/prompt/confirm_question.rb', line 73

def render_question
  header = "#{@prefix}#{message} "
  if !@done
    header += @prompt.decorate("(#{@suffix})", @help_color) + ' '
  else
    answer = convert_result(@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
# 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

Returns:

  • (Boolean)


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

def suffix?
  @suffix != UndefinedSetting
end