class TTY::Prompt::ConfirmQuestion

Public Class Methods

new(prompt, **options) click to toggle source

Create confirmation question

@param [Hash] options @option options [String] :suffix @option options [String] :positive @option options [String] :negative

@api public

Calls superclass method
# 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

Public Instance Methods

call(message, &block) click to toggle source
# 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)) click to toggle source

Set value for matching negative choice

@api public

# File lib/tty/prompt/confirm_question.rb, line 57
def negative(value = (not_set = true))
  return @negative if not_set

  @negative = value
end
negative?() click to toggle source
# File lib/tty/prompt/confirm_question.rb, line 28
def negative?
  @negative != UndefinedSetting
end
positive(value = (not_set = true)) click to toggle source

Set value for matching positive choice

@api public

# File lib/tty/prompt/confirm_question.rb, line 48
def positive(value = (not_set = true))
  return @positive if not_set

  @positive = value
end
positive?() click to toggle source
# File lib/tty/prompt/confirm_question.rb, line 24
def positive?
  @positive != UndefinedSetting
end
render_question() click to toggle source

Render confirmation question

@return [String]

@api private

# 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)) click to toggle source

Set question suffix

@api public

# File lib/tty/prompt/confirm_question.rb, line 39
def suffix(value = (not_set = true))
  return @negative if not_set

  @suffix = value
end
suffix?() click to toggle source
# File lib/tty/prompt/confirm_question.rb, line 32
def suffix?
  @suffix != UndefinedSetting
end

Protected Instance Methods

conversion() click to toggle source

Create custom conversion

@api private

# File lib/tty/prompt/confirm_question.rb, line 148
def conversion
  ->(input) do
    positive_word   = Regexp.escape(positive)
    positive_letter = Regexp.escape(positive[0])
    pattern = Regexp.new("^(#{positive_word}|#{positive_letter})$", true)
    !input.match(pattern).nil?
  end
end
create_default_labels() click to toggle source

@api private

# File lib/tty/prompt/confirm_question.rb, line 131
def create_default_labels
  @suffix   = default ? "Y/n" : "y/N"
  @positive = default ? "Yes" : "yes"
  @negative = default ? "no" : "No"
  @validation = /^(y(es)?|no?)$/i
  @messages[:valid?] = "Invalid input."
end
create_suffix() click to toggle source

@api private

# File lib/tty/prompt/confirm_question.rb, line 140
def create_suffix
  (default ? positive.capitalize : positive.downcase) + "/" +
    (default ? negative.downcase : negative.capitalize)
end
infer_default() click to toggle source

@api private

# File lib/tty/prompt/confirm_question.rb, line 121
def infer_default
  converted = Converters.convert(:bool, default.to_s)
  if converted == Const::Undefined
    raise InvalidArgument, "default needs to be `true` or `false`"
  else
    default(converted)
  end
end
process_input(question) click to toggle source

Decide how to handle input from user

@api private

# File lib/tty/prompt/confirm_question.rb, line 95
def process_input(question)
  @input = read_input(question)
  if Utils.blank?(@input)
    @input = default ? positive : negative
  end
  @evaluator.call(@input)
end
setup_defaults() click to toggle source

@api private

# File lib/tty/prompt/confirm_question.rb, line 104
def setup_defaults
  infer_default
  @convert = conversion
  return if suffix? && positive?

  if suffix? && (!positive? || !negative?)
    parts = @suffix.split("/")
    @positive = parts[0]
    @negative = parts[1]
  elsif !suffix? && positive?
    @suffix = create_suffix
  else
    create_default_labels
  end
end