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
# File 'lib/highline/question/answer_converter.rb', line 28

def convert
  self.answer = convert_by_answer_type if answer_type
  answer
end

#to_arrayArray

Returns answer converted to an Array.

Returns:

  • (Array)

    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_fileFile

Returns answer converted to a File.

Returns:

  • (File)

    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_floatFloat

Returns answer converted to a Float.

Returns:

  • (Float)

    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_integerInteger

Returns answer converted to an Integer.

Returns:

  • (Integer)

    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_pathnamePathname

Returns answer converted to an Pathname.

Returns:

  • (Pathname)

    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_procProc

Returns answer converted to an Proc.

Returns:

  • (Proc)

    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_regexpRegexp

Returns answer converted to a Regexp.

Returns:

  • (Regexp)

    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_stringHighLine::String

Returns answer converted to a HighLine::String.

Returns:



34
35
36
# File 'lib/highline/question/answer_converter.rb', line 34

def to_string
  HighLine::String(answer)
end

#to_symbolSymbol

Returns answer converted to an Symbol.

Returns:

  • (Symbol)

    answer converted to an Symbol



55
56
57
# File 'lib/highline/question/answer_converter.rb', line 55

def to_symbol
  answer.to_sym
end