summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-06-06 01:58:34 -0300
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-06-06 01:58:34 -0300
commitc1c69f85f89cd305456d1f50152dd7cbe285080c (patch)
tree1173d6f508cebb315db47e99db0f6ebf53ef5d7a
parentfbd248da4acf3d281c7672f2456afc7f0033b656 (diff)
downloadhighline-c1c69f85f89cd305456d1f50152dd7cbe285080c.tar.gz
Split some AnswerConverter conversions separate methods - simplify conditional
-rw-r--r--lib/highline/question/answer_converter.rb60
-rwxr-xr-xtest/test_highline.rb19
2 files changed, 61 insertions, 18 deletions
diff --git a/lib/highline/question/answer_converter.rb b/lib/highline/question/answer_converter.rb
index 3121246..cd2666f 100644
--- a/lib/highline/question/answer_converter.rb
+++ b/lib/highline/question/answer_converter.rb
@@ -21,25 +21,13 @@ class HighLine
return unless answer_type
self.answer =
- if [::String, HighLine::String].include?(answer_type)
- HighLine::String(answer)
- elsif [Float, Integer].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
+ if answer_type.respond_to? :parse
answer_type.parse(answer)
+ elsif answer_type.is_a? Class
+ send(answer_type.name.to_sym)
+ elsif answer_type.is_a?(Array)
+ self.answer = choices_complete(answer)
+ answer.last
elsif answer_type.is_a?(Proc)
answer_type.call(answer)
end
@@ -48,6 +36,42 @@ class HighLine
answer
end
+
+ def String
+ HighLine::String(answer)
+ end
+
+ # That's a weird name for a method!
+ # But it's working ;-)
+ define_method "HighLine::String" do
+ HighLine::String(answer)
+ end
+
+ def Integer
+ Kernel.send(:Integer, answer)
+ end
+
+ def Float
+ Kernel.send(:Float, answer)
+ end
+
+ def Symbol
+ answer.to_sym
+ end
+
+ def Regexp
+ Regexp.new(answer)
+ end
+
+ def File
+ self.answer = choices_complete(answer)
+ File.open(File.join(directory.to_s, answer.last))
+ end
+
+ def Pathname
+ self.answer = choices_complete(answer)
+ Pathname.new(File.join(directory.to_s, answer.last))
+ end
end
end
end \ No newline at end of file
diff --git a/test/test_highline.rb b/test/test_highline.rb
index 3c3e63f..04781f3 100755
--- a/test/test_highline.rb
+++ b/test/test_highline.rb
@@ -79,6 +79,25 @@ class TestHighLine < Minitest::Test
assert_raises(EOFError) { @terminal.ask("Any input left? ", String) }
end
+ def test_ask_string_converting
+ name = "James Edward Gray II"
+ @input << name << "\n"
+ @input.rewind
+
+ answer = @terminal.ask("What is your name? ", String)
+
+ assert_instance_of HighLine::String, answer
+
+ @input.rewind
+
+ answer = @terminal.ask("What is your name? ", HighLine::String)
+
+ assert_instance_of HighLine::String, answer
+
+ assert_raises(EOFError) { @terminal.ask("Any input left? ", HighLine::String) }
+ end
+
+
def test_indent
text = "Testing...\n"
@terminal.indent_level=1