diff options
author | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-06-05 00:39:43 -0300 |
---|---|---|
committer | Abinoam Praxedes Marques Jr <abinoam@gmail.com> | 2015-06-05 00:39:43 -0300 |
commit | 71f4f8f1d50fcf824966f24e19959272bf1ebb93 (patch) | |
tree | 5eb9da28132bb59cbebc0ce1bac3b9c82c781005 | |
parent | 69b1357c143be77edf6a366a419fa052e5ddb79f (diff) | |
download | highline-71f4f8f1d50fcf824966f24e19959272bf1ebb93.tar.gz |
Extract HighLine::Question::AnswerConverter
It makes the dependencies clear.
Preparing for refactoring this long chain of conditionals
into something not that long.
It also changes where NotInRangeQuestionError is raised,
simplifying HighLine a little bit.
-rwxr-xr-x | lib/highline.rb | 2 | ||||
-rwxr-xr-x | lib/highline/question.rb | 33 | ||||
-rw-r--r-- | lib/highline/question/answer_converter.rb | 53 |
3 files changed, 61 insertions, 27 deletions
diff --git a/lib/highline.rb b/lib/highline.rb index fa71793..98cdab9 100755 --- a/lib/highline.rb +++ b/lib/highline.rb @@ -531,8 +531,6 @@ class HighLine question.convert - raise NotInRangeQuestionError unless question.in_range? - if question.confirm # need to add a layer of scope (new_scope) to ask a question inside a # question, without destroying instance data diff --git a/lib/highline/question.rb b/lib/highline/question.rb index d48fcd2..664fac9 100755 --- a/lib/highline/question.rb +++ b/lib/highline/question.rb @@ -11,6 +11,7 @@ require "optparse" require "date" require "pathname" +require "highline/question/answer_converter" class HighLine # @@ -65,6 +66,8 @@ class HighLine build_responses end + attr_reader :directory + # The ERb template of the question to be asked. attr_accessor :template @@ -312,31 +315,11 @@ class HighLine # completed for any reason. # def convert - return unless @answer_type - - self.answer = - if [::String, HighLine::String].include?(@answer_type) - HighLine::String(answer) - elsif [Float, Integer, String].include?(@answer_type) - Kernel.send(@answer_type.to_s.to_sym, answer) - elsif @answer_type == Symbol - answer.to_sym - elsif @answer_type == Regexp - Regexp.new(answer) - elsif @answer_type.is_a?(Array) or [File, Pathname].include?(@answer_type) - self.answer = choices_complete(answer) - if @answer_type.is_a?(Array) - answer.last - elsif @answer_type == File - File.open(File.join(@directory.to_s, answer.last)) - else - Pathname.new(File.join(@directory.to_s, answer.last)) - end - elsif @answer_type.respond_to? :parse - @answer_type.parse(answer) - elsif @answer_type.is_a?(Proc) - @answer_type[answer] - end + Question::AnswerConverter.new(self).convert + end + + def check_range + raise NotInRangeQuestionError unless in_range? end def choices_complete(answer_string) diff --git a/lib/highline/question/answer_converter.rb b/lib/highline/question/answer_converter.rb new file mode 100644 index 0000000..0176854 --- /dev/null +++ b/lib/highline/question/answer_converter.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +# TODO: This module was extracted from HighLine::Question +# Now the dependencies are clear. See the delegators. +# Remember to refactor it! + +class HighLine + class Question + class AnswerConverter + extend Forwardable + + def_delegators :@question, + :answer, :answer=, :check_range, + :directory, :answer_type, :choices_complete + + def initialize(question) + @question = question + end + + def convert + return unless answer_type + + self.answer = + if [::String, HighLine::String].include?(answer_type) + HighLine::String(answer) + elsif [Float, Integer, String].include?(answer_type) + Kernel.send(answer_type.to_s.to_sym, answer) + elsif answer_type == Symbol + answer.to_sym + elsif answer_type == Regexp + Regexp.new(answer) + elsif answer_type.is_a?(Array) or [File, Pathname].include?(answer_type) + self.answer = choices_complete(answer) + if answer_type.is_a?(Array) + answer.last + elsif answer_type == File + File.open(File.join(directory.to_s, answer.last)) + else + Pathname.new(File.join(directory.to_s, answer.last)) + end + elsif answer_type.respond_to? :parse + answer_type.parse(answer) + elsif answer_type.is_a?(Proc) + answer_type[answer] + end + + check_range + + answer + end + end + end +end
\ No newline at end of file |