\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/highline-2.0.3/lib/highline/

Viewing File: question_asker.rb

# encoding: utf-8

class HighLine
  # Deals with the task of "asking" a question
  class QuestionAsker
    # @return [Question] question to be asked
    attr_reader :question

    include CustomErrors

    # To do its work QuestionAsker needs a Question
    # to be asked and a HighLine context where to
    # direct output.
    #
    # @param question [Question] question to be asked
    # @param highline [HighLine] context
    def initialize(question, highline)
      @question = question
      @highline = highline
    end

    #
    # Gets just one answer, as opposed to #gather_answers
    #
    # @return [String] answer
    def ask_once
      question.show_question(@highline)

      begin
        question.get_response_or_default(@highline)
        raise NotValidQuestionError unless question.valid_answer?

        question.convert

        if question.confirm
          confirmation = @highline.send(:confirm, question)
          raise NoConfirmationQuestionError unless confirmation
        end
      rescue ExplainableError => e
        explain_error(e.explanation_key)
        retry
      rescue ArgumentError => error
        case error.message
        when /ambiguous/
          # the assumption here is that OptionParser::Completion#complete
          # (used for ambiguity resolution) throws exceptions containing
          # the word 'ambiguous' whenever resolution fails
          explain_error(:ambiguous_completion)
          retry
        when /invalid value for/
          explain_error(:invalid_type)
          retry
        else
          raise
        end
      end

      question.answer
    end

    ## Multiple questions

    #
    # Collects an Array/Hash full of answers as described in
    # HighLine::Question.gather().
    #
    # @return [Array, Hash] answers
    def gather_answers
      verify_match = question.verify_match
      answers = []

      # when verify_match is set this loop will repeat until unique_answers == 1
      loop do
        answers = gather_answers_based_on_type

        break unless verify_match &&
                     (@highline.send(:unique_answers, answers).size > 1)

        explain_error(:mismatch)
      end

      verify_match ? @highline.send(:last_answer, answers) : answers
    end

    # Gather multiple integer values based on {Question#gather} count
    # @return [Array] answers
    def gather_integer
      gather_with_array do |answers|
        (question.gather - 1).times { answers << ask_once }
      end
    end

    # Gather multiple values until any of them matches the
    # {Question#gather} Regexp.
    # @return [Array] answers
    def gather_regexp
      gather_with_array do |answers|
        answers << ask_once until answer_matches_regex(answers.last)
        answers.pop
      end
    end

    # Gather multiple values and store them on a Hash
    # with keys provided by the Hash on {Question#gather}
    # @return [Hash]
    def gather_hash
      sorted_keys = question.gather.keys.sort_by(&:to_s)
      sorted_keys.each_with_object({}) do |key, answers|
        @highline.key = key
        answers[key]  = ask_once
      end
    end

    private

    ## Delegate to Highline
    def explain_error(explanation_key) # eg: :not_valid, :not_in_range
      @highline.say(question.final_response(explanation_key))
      @highline.say(question.ask_on_error_msg)
    end

    def gather_with_array
      [].tap do |answers|
        answers << ask_once
        question.template = ""

        yield answers
      end
    end

    def answer_matches_regex(answer)
      if question.gather.is_a?(::String) || question.gather.is_a?(Symbol)
        answer.to_s == question.gather.to_s
      elsif question.gather.is_a?(Regexp)
        answer.to_s =~ question.gather
      end
    end

    def gather_answers_based_on_type
      case question.gather
      when Integer
        gather_integer
      when ::String, Symbol, Regexp
        gather_regexp
      when Hash
        gather_hash
      end
    end
  end
end