class HighLine::Question::AnswerConverter

It provides all answer conversion flow.

Public Class Methods

new(question) click to toggle source

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

convert() click to toggle source

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
to_array() click to toggle source

@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
to_file() click to toggle source

@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
to_float() click to toggle source

@return [Float] answer converted to a Float

# File lib/highline/question/answer_converter.rb, line 53
def to_float
  Kernel.send(:Float, answer)
end
to_integer() click to toggle source

@return [Integer] answer converted to an Integer

# File lib/highline/question/answer_converter.rb, line 48
def to_integer
  Kernel.send(:Integer, answer)
end
to_pathname() click to toggle source

@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
to_proc() click to toggle source

@return [Proc] answer converted to an Proc

# File lib/highline/question/answer_converter.rb, line 86
def to_proc
  answer_type.call(answer)
end
to_regexp() click to toggle source

@return [Regexp] answer converted to a Regexp

# File lib/highline/question/answer_converter.rb, line 63
def to_regexp
  Regexp.new(answer)
end
to_string() click to toggle source

@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
to_symbol() click to toggle source

@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

convert_by_answer_type() click to toggle source
# 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