summaryrefslogtreecommitdiff
path: root/lib/highline/question_asker.rb
diff options
context:
space:
mode:
authormatugm <matugm@gmail.com>2015-09-23 18:51:39 +0200
committermatugm <matugm@gmail.com>2015-09-23 18:51:39 +0200
commit491737b74dae3640e241835c0d8b399fca150bf1 (patch)
treee817a3d62152ddc9731288c631d3b54c77bc73fb /lib/highline/question_asker.rb
parentda6efcb10f292286c028832f0cca5f48f60f2d02 (diff)
downloadhighline-491737b74dae3640e241835c0d8b399fca150bf1.tar.gz
Namespace errors and question asker
Diffstat (limited to 'lib/highline/question_asker.rb')
-rw-r--r--lib/highline/question_asker.rb231
1 files changed, 116 insertions, 115 deletions
diff --git a/lib/highline/question_asker.rb b/lib/highline/question_asker.rb
index 53b8988..f93731f 100644
--- a/lib/highline/question_asker.rb
+++ b/lib/highline/question_asker.rb
@@ -1,157 +1,158 @@
+class HighLine
+ class QuestionAsker
+ attr_reader :question
-class QuestionAsker
- attr_reader :question
+ include CustomErrors
- include CustomErrors
-
- def initialize(question, highline)
- @question = question
- @highline = highline
- end
-
- #
- # Gets one answer, as opposed to #gather
- #
- def ask_once
- # readline() needs to handle its own output, but readline only supports
- # full line reading. Therefore if question.echo is anything but true,
- # the prompt will not be issued. And we have to account for that now.
- # Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt
- # to handle line editing properly.
- @highline.say(question) unless ((question.readline) and (question.echo == true and !question.limit))
+ def initialize(question, highline)
+ @question = question
+ @highline = highline
+ end
- begin
- question.get_response_or_default(@highline)
- raise NotValidQuestionError unless question.valid_answer?
+ #
+ # Gets one answer, as opposed to #gather
+ #
+ def ask_once
+ # readline() needs to handle its own output, but readline only supports
+ # full line reading. Therefore if question.echo is anything but true,
+ # the prompt will not be issued. And we have to account for that now.
+ # Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt
+ # to handle line editing properly.
+ @highline.say(question) unless ((question.readline) and (question.echo == true and !question.limit))
- question.convert
+ begin
+ question.get_response_or_default(@highline)
+ raise NotValidQuestionError unless question.valid_answer?
- if question.confirm
- # need to add a layer of scope (new_scope) to ask a question inside a
- # question, without destroying instance data
+ question.convert
- raise NoConfirmationQuestionError unless @highline.send(:confirm, question)
- end
+ if question.confirm
+ # need to add a layer of scope (new_scope) to ask a question inside a
+ # question, without destroying instance data
- rescue NoConfirmationQuestionError
- explain_error(nil)
- retry
+ raise NoConfirmationQuestionError unless @highline.send(:confirm, question)
+ end
- rescue NotInRangeQuestionError
- explain_error(:not_in_range)
- retry
+ rescue NoConfirmationQuestionError
+ explain_error(nil)
+ retry
- rescue NotValidQuestionError
- explain_error(:not_valid)
- retry
+ rescue NotInRangeQuestionError
+ explain_error(:not_in_range)
+ retry
- rescue QuestionError
- retry
+ rescue NotValidQuestionError
+ explain_error(:not_valid)
+ 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)
+ rescue QuestionError
retry
- when /invalid value for/
- explain_error(:invalid_type)
+
+ 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
+
+ rescue NoAutoCompleteMatch
+ explain_error(:no_completion)
retry
- else
- raise
end
-
- rescue NoAutoCompleteMatch
- explain_error(:no_completion)
- retry
+ question.answer
end
- question.answer
- end
- ## Multiple questions
+ ## Multiple questions
- #
- # Collects an Array/Hash full of answers as described in
- # HighLine::Question.gather().
- #
+ #
+ # Collects an Array/Hash full of answers as described in
+ # HighLine::Question.gather().
+ #
- def gather_answers
- original_question_template = question.template
- verify_match = question.verify_match
+ def gather_answers
+ original_question_template = question.template
+ verify_match = question.verify_match
- begin # when verify_match is set this loop will repeat until unique_answers == 1
- question.template = original_question_template
+ begin # when verify_match is set this loop will repeat until unique_answers == 1
+ question.template = original_question_template
- answers =
- case question.gather
- when Integer
- gather_integer
- when ::String, Regexp
- gather_regexp
- when Hash
- gather_hash
- end
+ answers =
+ case question.gather
+ when Integer
+ gather_integer
+ when ::String, Regexp
+ gather_regexp
+ when Hash
+ gather_hash
+ end
- if verify_match && (@highline.send(:unique_answers, answers).size > 1)
- explain_error(:mismatch)
- else
- verify_match = false
- end
+ if verify_match && (@highline.send(:unique_answers, answers).size > 1)
+ explain_error(:mismatch)
+ else
+ verify_match = false
+ end
- end while verify_match
+ end while verify_match
- question.verify_match ? @highline.send(:last_answer, answers) : answers
- end
+ question.verify_match ? @highline.send(:last_answer, answers) : answers
+ end
- public :gather_answers
+ public :gather_answers
- def gather_integer
- answers = []
+ def gather_integer
+ answers = []
- answers << ask_once
+ answers << ask_once
- question.template = ""
+ question.template = ""
- (question.gather-1).times do
- answers << ask_once
+ (question.gather-1).times do
+ answers << ask_once
+ end
+
+ answers
end
- answers
- end
+ def gather_regexp
+ answers = []
- def gather_regexp
- answers = []
+ answers << ask_once
- answers << ask_once
+ question.template = ""
+ until (question.gather.is_a?(::String) and answers.last.to_s == question.gather) or
+ (question.gather.is_a?(Regexp) and answers.last.to_s =~ question.gather)
+ answers << ask_once
+ end
- question.template = ""
- until (question.gather.is_a?(::String) and answers.last.to_s == question.gather) or
- (question.gather.is_a?(Regexp) and answers.last.to_s =~ question.gather)
- answers << ask_once
+ answers.pop
+ answers
end
- answers.pop
- answers
- end
-
- def gather_hash
- answers = {}
+ def gather_hash
+ answers = {}
- question.gather.keys.sort.each do |key|
- @highline.key = key
- answers[key] = ask_once
+ question.gather.keys.sort.each do |key|
+ @highline.key = key
+ answers[key] = ask_once
+ end
+ answers
end
- answers
- end
- ## Delegate to Highline
+ ## Delegate to Highline
- private
+ private
- def explain_error(error)
- @highline.say(question.responses[error]) if error
- @highline.say(question.ask_on_error_msg)
+ def explain_error(error)
+ @highline.say(question.responses[error]) if error
+ @highline.say(question.ask_on_error_msg)
+ end
end
end