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 return unless answer_type self.answer = convert_by_answer_type check_range answer end
@return [Array] answer converted to an Array
# File lib/highline/question/answer_converter.rb, line 80 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 68 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 53 def to_float Kernel.send(:Float, answer) end
@return [Integer] answer converted to an Integer
# File lib/highline/question/answer_converter.rb, line 48 def to_integer Kernel.send(:Integer, answer) end
@return [Pathname] answer converted to an Pathname
# File lib/highline/question/answer_converter.rb, line 74 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 86 def to_proc answer_type.call(answer) end
@return [Regexp] answer converted to a Regexp
# File lib/highline/question/answer_converter.rb, line 63 def to_regexp Regexp.new(answer) end
@return [HighLine::String] answer converted to a HighLine::String
# File lib/highline/question/answer_converter.rb, line 37 def to_string HighLine::String(answer) end
@return [Symbol] answer converted to an Symbol
# File lib/highline/question/answer_converter.rb, line 58 def to_symbol answer.to_sym end
Private Instance Methods
# File lib/highline/question/answer_converter.rb, line 92 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