Class: HighLine::QuestionAsker
- Includes:
- CustomErrors
- Defined in:
- lib/highline/question_asker.rb
Overview
Deals with the task of “asking” a question
Instance Attribute Summary collapse
-
#question ⇒ Question
readonly
Question to be asked.
Instance Method Summary collapse
-
#ask_once ⇒ String
Gets just one answer, as opposed to #gather_answers.
-
#gather_answers ⇒ Array, Hash
Collects an Array/Hash full of answers as described in HighLine::Question.gather().
-
#gather_hash ⇒ Hash
Gather multiple values and store them on a Hash with keys provided by the Hash on HighLine::Question#gather.
-
#gather_integer ⇒ Array
Gather multiple integer values based on HighLine::Question#gather count.
-
#gather_regexp ⇒ Array
Gather multiple values until any of them matches the HighLine::Question#gather Regexp.
-
#initialize(question, highline) ⇒ QuestionAsker
constructor
To do its work QuestionAsker needs a Question to be asked and a HighLine context where to direct output.
Constructor Details
#initialize(question, highline) ⇒ QuestionAsker
To do its work QuestionAsker needs a Question to be asked and a HighLine context where to direct output.
17 18 19 20 |
# File 'lib/highline/question_asker.rb', line 17 def initialize(question, highline) @question = question @highline = highline end |
Instance Attribute Details
#question ⇒ Question (readonly)
Returns question to be asked.
7 8 9 |
# File 'lib/highline/question_asker.rb', line 7 def question @question end |
Instance Method Details
#ask_once ⇒ String
Gets just one answer, as opposed to #gather_answers
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/highline/question_asker.rb', line 26 def ask_once # If in readline mode, let reline take care of the prompt question.show_question(@highline) unless question.readline begin question.get_response_or_default(@highline) raise NotValidQuestionError unless question.valid_answer? question.convert question.check_range 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. 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 |
#gather_answers ⇒ Array, Hash
Collects an Array/Hash full of answers as described in HighLine::Question.gather().
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/highline/question_asker.rb', line 70 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_hash ⇒ Hash
Gather multiple values and store them on a Hash with keys provided by the Hash on HighLine::Question#gather
108 109 110 111 112 113 114 |
# File 'lib/highline/question_asker.rb', line 108 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 |
#gather_integer ⇒ Array
Gather multiple integer values based on HighLine::Question#gather count
89 90 91 92 93 |
# File 'lib/highline/question_asker.rb', line 89 def gather_integer gather_with_array do |answers| (question.gather - 1).times { answers << ask_once } end end |
#gather_regexp ⇒ Array
Gather multiple values until any of them matches the HighLine::Question#gather Regexp.
98 99 100 101 102 103 |
# File 'lib/highline/question_asker.rb', line 98 def gather_regexp gather_with_array do |answers| answers << ask_once until answer_matches_regex(answers.last) answers.pop end end |