Class: HighLine::Question::AnswerConverter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/highline/question/answer_converter.rb

Overview

It provides all answer conversion flow.

Instance Method Summary collapse

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

Parameters:



21
22
23
# File 'lib/highline/question/answer_converter.rb', line 21

def initialize(question)
  @question = question
end

Instance Method Details

#convertObject

Based on the given Question object’s settings, it makes the conversion and returns the answer.

Returns:

  • (Object)

    the converted answer.



28
29
30
31
32
33
34
# 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_arrayArray

Returns answer converted to an Array.

Returns:

  • (Array)

    answer converted to an Array



80
81
82
83
# File 'lib/highline/question/answer_converter.rb', line 80

def to_array
  self.answer = choices_complete(answer)
  answer.last
end

#to_fileFile

Returns answer converted to a File.

Returns:

  • (File)

    answer converted to a File



68
69
70
71
# 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_floatFloat

Returns answer converted to a Float.

Returns:

  • (Float)

    answer converted to a Float



53
54
55
# File 'lib/highline/question/answer_converter.rb', line 53

def to_float
  Kernel.send(:Float, answer)
end

#to_integerInteger

Returns answer converted to an Integer.

Returns:

  • (Integer)

    answer converted to an Integer



48
49
50
# File 'lib/highline/question/answer_converter.rb', line 48

def to_integer
  Kernel.send(:Integer, answer)
end

#to_pathnamePathname

Returns answer converted to an Pathname.

Returns:

  • (Pathname)

    answer converted to an Pathname



74
75
76
77
# 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_procProc

Returns answer converted to an Proc.

Returns:

  • (Proc)

    answer converted to an Proc



86
87
88
# File 'lib/highline/question/answer_converter.rb', line 86

def to_proc
  answer_type.call(answer)
end

#to_regexpRegexp

Returns answer converted to a Regexp.

Returns:

  • (Regexp)

    answer converted to a Regexp



63
64
65
# File 'lib/highline/question/answer_converter.rb', line 63

def to_regexp
  Regexp.new(answer)
end

#to_stringHighLine::String

Returns answer converted to a HighLine::String.

Returns:



37
38
39
# File 'lib/highline/question/answer_converter.rb', line 37

def to_string
  HighLine::String(answer)
end

#to_symbolSymbol

Returns answer converted to an Symbol.

Returns:

  • (Symbol)

    answer converted to an Symbol



58
59
60
# File 'lib/highline/question/answer_converter.rb', line 58

def to_symbol
  answer.to_sym
end