class HighLine::Question::AnswerConverter
It provides all answer conversion flow.
Public Class Methods
It should be initialized with a Question
object. The class will get the answer from {Question#answer} and then convert it to the proper {Question#answer_type}. It is mainly used by {Question#convert}
@param question [Question]
# File lib/highline/question/answer_converter.rb, line 21 def initialize(question) @question = question end
Public Instance Methods
Based on the given Question
object's settings, it makes the conversion and returns the answer. @return [Object] the converted answer.
# File lib/highline/question/answer_converter.rb, line 28 def convert self.answer = convert_by_answer_type if answer_type answer end
@return [Array] answer converted to an Array
# File lib/highline/question/answer_converter.rb, line 77 def to_array self.answer = choices_complete(answer) answer.last end
@return [File] answer converted to a File
# 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
@return [Float] answer converted to a Float
# File lib/highline/question/answer_converter.rb, line 50 def to_float Kernel.send(:Float, answer) end
@return [Integer] answer converted to an Integer
# File lib/highline/question/answer_converter.rb, line 45 def to_integer Kernel.send(:Integer, answer) end
@return [Pathname] answer converted to an Pathname
# 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
@return [Proc] answer converted to an Proc
# File lib/highline/question/answer_converter.rb, line 83 def to_proc answer_type.call(answer) end
@return [Regexp] answer converted to a Regexp
# File lib/highline/question/answer_converter.rb, line 60 def to_regexp Regexp.new(answer) end
@return [HighLine::String] answer converted to a HighLine::String
# File lib/highline/question/answer_converter.rb, line 34 def to_string HighLine::String(answer) end
@return [Symbol] answer converted to an Symbol
# File lib/highline/question/answer_converter.rb, line 55 def to_symbol answer.to_sym end
Private Instance Methods
# File lib/highline/question/answer_converter.rb, line 89 def convert_by_answer_type if answer_type.respond_to? :parse answer_type.parse(answer) elsif answer_type.is_a? Class send("to_#{answer_type.name.downcase}") else send("to_#{answer_type.class.name.downcase}") end end