-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b51aacc
commit cadf671
Showing
17 changed files
with
457 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"simplecov-vscode.enabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'utils/key' | ||
require 'utils/array' | ||
require 'utils/message' | ||
require 'utils/question' | ||
require 'resources/path_helper' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(Yes/No/Y/N) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
You need to answer with "Yes or No" only. | ||
These are the valid response options: Yes, No, Y, N | ||
Note: You can use both lowercase and uppercase letters for the answers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
You need to provide a numeric value with decimal places. | ||
Example: 3143.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The index value needs to be within the above listing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
You need to provide an integer numeric value. | ||
Example: 123456 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Message does not meet the requested standard. | ||
Please provide a message within the expected standard. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# Extension to Ruby's Array class to enhance functionality. | ||
class Array | ||
# Lists all elements in the array with their index. | ||
# | ||
# This method outputs each element of the array to the console, prefixed by its index (1-based). | ||
# The index is right-justified based on the length of the array, ensuring a tidy, column-aligned output. | ||
# | ||
# Example output for a 3-element array: | ||
# |1| Element 1 | ||
# |2| Element 2 | ||
# |3| Element 3 | ||
# | ||
# @return [void] | ||
def list_all_elements | ||
index_size = count.to_s.length | ||
each_index do |index| | ||
puts "|#{(index + 1).to_s.rjust(index_size)}| #{self[index]}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'message' | ||
require_relative 'array' | ||
|
||
# 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. | ||
class Question | ||
# Initializes a new instance of the Question class. | ||
# | ||
# @param message [String] The question message to be displayed to the user. | ||
def initialize(message) | ||
@message = Message.new(message) | ||
end | ||
|
||
# Prompts the user with a boolean question and returns the answer as true or false. | ||
# | ||
# @param error_message [String, nil] Custom error message for invalid inputs. | ||
# @return [Boolean] True if the user answers affirmatively, otherwise false. | ||
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 | ||
|
||
# Prompts the user for a floating-point number and returns the value. | ||
# | ||
# @param error_message [String, nil] Custom error message for invalid inputs. | ||
# @return [Float] The user's input converted to a float. | ||
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 | ||
|
||
# Prompts the user for an integer and returns the value. | ||
# | ||
# @param error_message [String, nil] Custom error message for invalid inputs. | ||
# @return [Integer] The user's input converted to an integer. | ||
def integer_answer(error_message: nil) | ||
error = error_message || File.join('question', 'error', 'integer') | ||
string_answer(regex: /^\d+$/, error_message: error).to_i | ||
end | ||
|
||
# Prompts the user to select an option from a given list and returns the selected option. | ||
# | ||
# @param options [Array] The list of options for the user to choose from. | ||
# @param error_message [String, nil] Custom error message for invalid inputs. | ||
# @raise [RuntimeError] If options is not an Array or is empty. | ||
# @return [Object] The selected option from the list. If options count is 1 return the first element. | ||
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 | ||
|
||
# Prompts the user for a string that matches a given regular expression. | ||
# | ||
# @param regex [Regexp, nil] The regular expression the user's input must match. | ||
# @param error_message [String, nil] Custom error message for invalid inputs. | ||
# @return [String] The user's input if it matches the given regex. | ||
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 | ||
|
||
private | ||
|
||
# Displays a message to the user and returns their input, if it satisfies the given condition. | ||
# | ||
# @param message [String] The message to display to the user. | ||
# @param error_message [String] The error message to display for invalid inputs. | ||
# @yieldparam input [String] The user's input to validate. | ||
# @yieldreturn [Boolean] True if the input is valid, otherwise false. | ||
# @return [String] The user's valid input. | ||
def read_input(message = @message, error_message:) | ||
loop do | ||
puts message | ||
input = $stdin.gets.chomp | ||
return input if yield input | ||
|
||
puts Message.new(error_message) | ||
end | ||
end | ||
|
||
# Reads an index input from the user, ensuring it falls within a specified range. | ||
# | ||
# This method prompts the user to enter an index number. It validates the input to ensure | ||
# it is an integer within the range of 0 to (count - 1). If the input is invalid, an error | ||
# message is displayed, and the user is prompted again. | ||
# | ||
# @param count [Integer] The count of items, setting the upper limit of the valid index range. | ||
# @param error_message [String, nil] Custom error message for invalid index inputs. | ||
# Defaults to the path 'question/error/index' if nil. | ||
# @return [Integer] The user's input adjusted to be zero-based (input - 1) and validated to be within the range. | ||
def read_index_input(count:, error_message: nil) | ||
loop do | ||
index = integer_answer(error_message: error_message) - 1 | ||
return index if index >= 0 && index < count | ||
|
||
error = error_message || File.join('question', 'error', 'index') | ||
puts Message.new(error) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module MPUtils | ||
VERSION = '0.1.3' | ||
VERSION = '0.2.1' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../lib/utils/array' | ||
|
||
RSpec.describe Array do | ||
describe '#list_all_elements' do | ||
context 'when the array is empty' do | ||
it 'prints nothing' do | ||
expect { [].list_all_elements }.to output('').to_stdout | ||
end | ||
end | ||
|
||
context 'when the array has one element' do | ||
it 'prints the element with its index' do | ||
expect { ['Element'].list_all_elements }.to output("|1| Element\n").to_stdout | ||
end | ||
end | ||
|
||
context 'when the array has multiple elements' do | ||
it 'prints all elements with their indices, aligned properly' do | ||
array = %w[First Second Third] | ||
expected_output = <<~OUTPUT | ||
|1| First | ||
|2| Second | ||
|3| Third | ||
OUTPUT | ||
expect { array.list_all_elements }.to output(expected_output).to_stdout | ||
end | ||
end | ||
|
||
context 'with a larger set of elements affecting index alignment' do | ||
it 'adjusts the index alignment based on the number of elements' do | ||
array = (1..12).to_a | ||
expected_output = <<~OUTPUT | ||
| 1| 1 | ||
| 2| 2 | ||
| 3| 3 | ||
| 4| 4 | ||
| 5| 5 | ||
| 6| 6 | ||
| 7| 7 | ||
| 8| 8 | ||
| 9| 9 | ||
|10| 10 | ||
|11| 11 | ||
|12| 12 | ||
OUTPUT | ||
expect { array.list_all_elements }.to output(expected_output).to_stdout | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.