Class: Question

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/question.rb

Overview

The Question class facilitates the creation and management of interactive questions in the console. It provides methods to validate and return user input as various data types including boolean, float, integer, options (from a list), and string.

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ Question

Initializes a new instance of the Question class.

Parameters:

  • message (String)

    The question message to be displayed to the user.



13
14
15
# File 'lib/utils/question.rb', line 13

def initialize(message)
  @message = Message.new(message)
end

Instance Method Details

#bool_answer(error_message: nil) ⇒ Boolean

Prompts the user with a boolean question and returns the answer as true or false.

Parameters:

  • error_message (String, nil) (defaults to: nil)

    Custom error message for invalid inputs.

Returns:

  • (Boolean)

    True if the user answers affirmatively, otherwise false.



21
22
23
24
25
26
27
28
29
# File 'lib/utils/question.rb', line 21

def bool_answer(error_message: nil)
  error = error_message || File.join('question', 'error', 'bool')
  bool_sufix = Message.new(File.join('question', 'bool_sufix'))
  result = read_input("#{@message} #{bool_sufix}", error_message: error) do |input|
    input.match?(/^((Y|y)((E|e)(S|s))*)|((N|n)(O|o)*)$/)
  end

  result.match?(/^(Y|y)((E|e)(S|s))*$/)
end

#float_answer(error_message: nil) ⇒ Float

Prompts the user for a floating-point number and returns the value.

Parameters:

  • error_message (String, nil) (defaults to: nil)

    Custom error message for invalid inputs.

Returns:

  • (Float)

    The user's input converted to a float.



35
36
37
38
# File 'lib/utils/question.rb', line 35

def float_answer(error_message: nil)
  error = error_message || File.join('question', 'error', 'float')
  string_answer(regex: /^\d+\.\d+$/, error_message: error).to_f
end

#integer_answer(error_message: nil) ⇒ Integer

Prompts the user for an integer and returns the value.

Parameters:

  • error_message (String, nil) (defaults to: nil)

    Custom error message for invalid inputs.

Returns:

  • (Integer)

    The user's input converted to an integer.



44
45
46
47
# File 'lib/utils/question.rb', line 44

def integer_answer(error_message: nil)
  error = error_message || File.join('question', 'error', 'integer')
  string_answer(regex: /^\d+$/, error_message: error).to_i
end

#option_answer(options, error_message: nil) ⇒ Object

Prompts the user to select an option from a given list and returns the selected option.

Parameters:

  • options (Array)

    The list of options for the user to choose from.

  • error_message (String, nil) (defaults to: nil)

    Custom error message for invalid inputs.

Returns:

  • (Object)

    The selected option from the list. If options count is 1 return the first element.

Raises:

  • (RuntimeError)

    If options is not an Array or is empty.



55
56
57
58
59
60
61
62
63
# File 'lib/utils/question.rb', line 55

def option_answer(options, error_message: nil)
  raise 'Options should be an Array' unless options.is_a?(Array)
  raise 'Options should not be empty' if options.empty?
  return options.first if options.count == 1

  options.list_all_elements
  index = read_index_input(count: options.count, error_message: error_message)
  options[index]
end

#string_answer(regex: nil, error_message: nil) ⇒ String

Prompts the user for a string that matches a given regular expression.

Parameters:

  • regex (Regexp, nil) (defaults to: nil)

    The regular expression the user's input must match.

  • error_message (String, nil) (defaults to: nil)

    Custom error message for invalid inputs.

Returns:

  • (String)

    The user's input if it matches the given regex.



70
71
72
73
74
75
# File 'lib/utils/question.rb', line 70

def string_answer(regex: nil, error_message: nil)
  error = error_message || File.join('question', 'error', 'regex')
  read_input(error_message: error) do |input|
    regex.nil? || input.match?(regex)
  end
end