Class: HighLine::Question::AnswerConverter
- Extended by:
- Forwardable
- Defined in:
- lib/highline/question/answer_converter.rb
Overview
It provides all answer conversion flow.
Instance Method Summary collapse
-
#convert ⇒ Object
Based on the given Question object’s settings, it makes the conversion and returns the answer.
-
#initialize(question) ⇒ AnswerConverter
constructor
It should be initialized with a Question object.
-
#to_array ⇒ Array
Answer converted to an Array.
-
#to_file ⇒ File
Answer converted to a File.
-
#to_float ⇒ Float
Answer converted to a Float.
-
#to_integer ⇒ Integer
Answer converted to an Integer.
-
#to_pathname ⇒ Pathname
Answer converted to an Pathname.
-
#to_proc ⇒ Proc
Answer converted to an Proc.
-
#to_regexp ⇒ Regexp
Answer converted to a Regexp.
-
#to_string ⇒ HighLine::String
Answer converted to a HighLine::String.
-
#to_symbol ⇒ Symbol
Answer converted to an Symbol.
Constructor Details
#initialize(question) ⇒ AnswerConverter
It should be initialized with a Question object. The class will get the answer from HighLine::Question#answer and then convert it to the proper HighLine::Question#answer_type. It is mainly used by HighLine::Question#convert
21 22 23 |
# File 'lib/highline/question/answer_converter.rb', line 21 def initialize(question) @question = question end |
Instance Method Details
#convert ⇒ Object
Based on the given Question object’s settings, it makes the conversion and returns the answer.
28 29 30 31 |
# File 'lib/highline/question/answer_converter.rb', line 28 def convert self.answer = convert_by_answer_type if answer_type answer end |
#to_array ⇒ Array
Returns answer converted to an Array.
77 78 79 80 |
# File 'lib/highline/question/answer_converter.rb', line 77 def to_array self.answer = choices_complete(answer) answer.last end |
#to_file ⇒ File
Returns answer converted to a File.
65 66 67 68 |
# File 'lib/highline/question/answer_converter.rb', line 65 def to_file self.answer = choices_complete(answer) File.open(File.join(directory.to_s, answer.last)) end |
#to_float ⇒ Float
Returns answer converted to a Float.
50 51 52 |
# File 'lib/highline/question/answer_converter.rb', line 50 def to_float Kernel.send(:Float, answer) end |
#to_integer ⇒ Integer
Returns answer converted to an Integer.
45 46 47 |
# File 'lib/highline/question/answer_converter.rb', line 45 def to_integer Kernel.send(:Integer, answer) end |
#to_pathname ⇒ Pathname
Returns answer converted to an Pathname.
71 72 73 74 |
# File 'lib/highline/question/answer_converter.rb', line 71 def to_pathname self.answer = choices_complete(answer) Pathname.new(File.join(directory.to_s, answer.last)) end |
#to_proc ⇒ Proc
Returns answer converted to an Proc.
83 84 85 |
# File 'lib/highline/question/answer_converter.rb', line 83 def to_proc answer_type.call(answer) end |
#to_regexp ⇒ Regexp
Returns answer converted to a Regexp.
60 61 62 |
# File 'lib/highline/question/answer_converter.rb', line 60 def to_regexp Regexp.new(answer) end |
#to_string ⇒ HighLine::String
Returns answer converted to a HighLine::String.
34 35 36 |
# File 'lib/highline/question/answer_converter.rb', line 34 def to_string HighLine::String(answer) end |
#to_symbol ⇒ Symbol
Returns answer converted to an Symbol.
55 56 57 |
# File 'lib/highline/question/answer_converter.rb', line 55 def to_symbol answer.to_sym end |