mp-utils 0.1.3 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mp_utils.rb +2 -0
- data/lib/resources/messages/question/bool_sufix.txt +1 -0
- data/lib/resources/messages/question/error/bool.txt +3 -0
- data/lib/resources/messages/question/error/float.txt +2 -0
- data/lib/resources/messages/question/error/index.txt +1 -0
- data/lib/resources/messages/question/error/integer.txt +2 -0
- data/lib/resources/messages/question/error/regex.txt +2 -0
- data/lib/resources/path_helper.rb +1 -1
- data/lib/utils/array.rb +22 -0
- data/lib/utils/message.rb +39 -25
- data/lib/utils/question.rb +115 -0
- metadata +11 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eaca69fbff0b08c6113af4c650ce37b158779bd260457fcd6e8b125dd4b4fee5
|
4
|
+
data.tar.gz: db821389f518caaaf4e4c130f92e8bf01e4ca2482f94683727f515363b332970
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac347a457eb76b4cec8fd440e69fa37503e463f23c30570b5cb6aae8647b52f6327dd4b2c48734ce6132d0d4ebf322582100aac39f6ed79bab495f85b059fc76
|
7
|
+
data.tar.gz: 5137278119e0ce6b5c6f54a838526eedee624eec8bb4b45f9e0bcfa4f74546f0a460863af489b880edee14231947e89d606426870944a696f090ec0e4062e4b8
|
data/lib/mp_utils.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
(Yes/No/Y/N)
|
@@ -0,0 +1 @@
|
|
1
|
+
The index value needs to be within the above listing.
|
@@ -18,7 +18,7 @@ module Resources
|
|
18
18
|
# {define} method. This method allows for easy retrieval of the custom path, facilitating flexible
|
19
19
|
# resource management.
|
20
20
|
#
|
21
|
-
# @return [String, nil] the custom
|
21
|
+
# @return [String, nil] the custom path as defined by the environment variable, or nil if it hasn't been set.
|
22
22
|
def self.custom_path
|
23
23
|
ENV['SCRIPT_CUSTOM_RESOURCES']
|
24
24
|
end
|
data/lib/utils/array.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Extension to Ruby's Array class to enhance functionality.
|
4
|
+
class Array
|
5
|
+
# Lists all elements in the array with their index.
|
6
|
+
#
|
7
|
+
# This method outputs each element of the array to the console, prefixed by its index (1-based).
|
8
|
+
# The index is right-justified based on the length of the array, ensuring a tidy, column-aligned output.
|
9
|
+
#
|
10
|
+
# Example output for a 3-element array:
|
11
|
+
# |1| Element 1
|
12
|
+
# |2| Element 2
|
13
|
+
# |3| Element 3
|
14
|
+
#
|
15
|
+
# @return [void]
|
16
|
+
def list_all_elements
|
17
|
+
index_size = count.to_s.length
|
18
|
+
each_index do |index|
|
19
|
+
puts "|#{(index + 1).to_s.rjust(index_size)}| #{self[index]}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/utils/message.rb
CHANGED
@@ -20,10 +20,11 @@ class Message
|
|
20
20
|
# Initializes a new instance of the Message class with a given message template.
|
21
21
|
#
|
22
22
|
# @param message [String] The message template to be used.
|
23
|
-
# @param replaces [
|
23
|
+
# @param replaces [Hash] Used to replace all occurrences of a key with its value.
|
24
24
|
def initialize(message, replaces: nil)
|
25
25
|
raise 'Messsage replaces content need be a Hash' if !replaces.nil? && !replaces.is_a?(Hash)
|
26
|
-
|
26
|
+
|
27
|
+
@to_replace = replaces
|
27
28
|
@message = message
|
28
29
|
end
|
29
30
|
|
@@ -42,29 +43,8 @@ class Message
|
|
42
43
|
#
|
43
44
|
# @return [String] The formatted message with placeholders substituted with actual content.
|
44
45
|
def to_s
|
45
|
-
new_message = String.new(@message)
|
46
|
-
|
47
|
-
|
48
|
-
if keys.count.positive?
|
49
|
-
keys.each do |key|
|
50
|
-
message = recover_message_with(key.value)
|
51
|
-
new_message.gsub!(key.to_s, message) if message != key.value
|
52
|
-
end
|
53
|
-
else
|
54
|
-
new_message = recover_message_with(new_message)
|
55
|
-
end
|
56
|
-
|
57
|
-
if !@replaces.nil?
|
58
|
-
@replaces.each do |key, value|
|
59
|
-
if key.is_a?(Key)
|
60
|
-
new_message.gsub!(key.to_regexp, value.to_s)
|
61
|
-
else
|
62
|
-
new_message.gsub!(/#{Regexp.escape(key.to_s)}/, value.to_s)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
new_message
|
46
|
+
new_message = replace_message_keys(String.new(@message))
|
47
|
+
replace_all_to_replace_elements(new_message)
|
68
48
|
end
|
69
49
|
end
|
70
50
|
|
@@ -90,6 +70,40 @@ class Message
|
|
90
70
|
File.join(Resources.library_path, 'messages')
|
91
71
|
end
|
92
72
|
|
73
|
+
# Replaces keys found in the original message with their corresponding values.
|
74
|
+
#
|
75
|
+
# @param message [String] The original message with keys to be replaced.
|
76
|
+
# @return [String] The message with keys replaced by their corresponding values.
|
77
|
+
def replace_message_keys(message)
|
78
|
+
keys = Key.find_keys_in(message)
|
79
|
+
return recover_message_with(message) if keys.empty?
|
80
|
+
|
81
|
+
keys.each do |key|
|
82
|
+
key_message = recover_message_with(key.value)
|
83
|
+
message.gsub!(key.to_s, key_message) if key_message != key.value
|
84
|
+
end
|
85
|
+
|
86
|
+
message
|
87
|
+
end
|
88
|
+
|
89
|
+
# Replaces all placeholders in the message with their corresponding values from the @to_replace hash.
|
90
|
+
#
|
91
|
+
# @param message [String] The message with placeholders to replace.
|
92
|
+
# @return [String] The message with all placeholders replaced with actual content.
|
93
|
+
def replace_all_to_replace_elements(message)
|
94
|
+
return message if @to_replace.nil?
|
95
|
+
|
96
|
+
@to_replace.each do |key, value|
|
97
|
+
if key.is_a?(Key)
|
98
|
+
message.gsub!(key.to_regexp, value.to_s)
|
99
|
+
else
|
100
|
+
message.gsub!(/#{Regexp.escape(key.to_s)}/, value.to_s)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
message
|
105
|
+
end
|
106
|
+
|
93
107
|
# Attempts to recover and return the content of a message file identified by the file_name parameter.
|
94
108
|
# It first looks in the custom path (if defined) and then in the library path.
|
95
109
|
#
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'message'
|
4
|
+
require_relative 'array'
|
5
|
+
|
6
|
+
# The Question class facilitates the creation and management of interactive questions in the console.
|
7
|
+
# It provides methods to validate and return user input as various data types including boolean,
|
8
|
+
# float, integer, options (from a list), and string.
|
9
|
+
class Question
|
10
|
+
# Initializes a new instance of the Question class.
|
11
|
+
#
|
12
|
+
# @param message [String] The question message to be displayed to the user.
|
13
|
+
def initialize(message)
|
14
|
+
@message = Message.new(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Prompts the user with a boolean question and returns the answer as true or false.
|
18
|
+
#
|
19
|
+
# @param error_message [String, nil] Custom error message for invalid inputs.
|
20
|
+
# @return [Boolean] True if the user answers affirmatively, otherwise false.
|
21
|
+
def bool_answer(error_message: nil)
|
22
|
+
error = error_message || File.join('question', 'error', 'bool')
|
23
|
+
bool_sufix = Message.new(File.join('question', 'bool_sufix'))
|
24
|
+
result = read_input("#{@message} #{bool_sufix}", error_message: error) do |input|
|
25
|
+
input.match?(/^((Y|y)((E|e)(S|s))*)|((N|n)(O|o)*)$/)
|
26
|
+
end
|
27
|
+
|
28
|
+
result.match?(/^(Y|y)((E|e)(S|s))*$/)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Prompts the user for a floating-point number and returns the value.
|
32
|
+
#
|
33
|
+
# @param error_message [String, nil] Custom error message for invalid inputs.
|
34
|
+
# @return [Float] The user's input converted to a float.
|
35
|
+
def float_answer(error_message: nil)
|
36
|
+
error = error_message || File.join('question', 'error', 'float')
|
37
|
+
string_answer(regex: /^\d+\.\d+$/, error_message: error).to_f
|
38
|
+
end
|
39
|
+
|
40
|
+
# Prompts the user for an integer and returns the value.
|
41
|
+
#
|
42
|
+
# @param error_message [String, nil] Custom error message for invalid inputs.
|
43
|
+
# @return [Integer] The user's input converted to an integer.
|
44
|
+
def integer_answer(error_message: nil)
|
45
|
+
error = error_message || File.join('question', 'error', 'integer')
|
46
|
+
string_answer(regex: /^\d+$/, error_message: error).to_i
|
47
|
+
end
|
48
|
+
|
49
|
+
# Prompts the user to select an option from a given list and returns the selected option.
|
50
|
+
#
|
51
|
+
# @param options [Array] The list of options for the user to choose from.
|
52
|
+
# @param error_message [String, nil] Custom error message for invalid inputs.
|
53
|
+
# @raise [RuntimeError] If options is not an Array or is empty.
|
54
|
+
# @return [Object] The selected option from the list. If options count is 1 return the first element.
|
55
|
+
def option_answer(options, error_message: nil)
|
56
|
+
raise 'Options should be an Array' unless options.is_a?(Array)
|
57
|
+
raise 'Options should not be empty' if options.empty?
|
58
|
+
return options.first if options.count == 1
|
59
|
+
|
60
|
+
options.list_all_elements
|
61
|
+
index = read_index_input(count: options.count, error_message: error_message)
|
62
|
+
options[index]
|
63
|
+
end
|
64
|
+
|
65
|
+
# Prompts the user for a string that matches a given regular expression.
|
66
|
+
#
|
67
|
+
# @param regex [Regexp, nil] The regular expression the user's input must match.
|
68
|
+
# @param error_message [String, nil] Custom error message for invalid inputs.
|
69
|
+
# @return [String] The user's input if it matches the given regex.
|
70
|
+
def string_answer(regex: nil, error_message: nil)
|
71
|
+
error = error_message || File.join('question', 'error', 'regex')
|
72
|
+
read_input(error_message: error) do |input|
|
73
|
+
regex.nil? || input.match?(regex)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
# Displays a message to the user and returns their input, if it satisfies the given condition.
|
80
|
+
#
|
81
|
+
# @param message [String] The message to display to the user.
|
82
|
+
# @param error_message [String] The error message to display for invalid inputs.
|
83
|
+
# @yieldparam input [String] The user's input to validate.
|
84
|
+
# @yieldreturn [Boolean] True if the input is valid, otherwise false.
|
85
|
+
# @return [String] The user's valid input.
|
86
|
+
def read_input(message = @message, error_message:)
|
87
|
+
loop do
|
88
|
+
puts message
|
89
|
+
input = $stdin.gets.chomp
|
90
|
+
return input if yield input
|
91
|
+
|
92
|
+
puts Message.new(error_message)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Reads an index input from the user, ensuring it falls within a specified range.
|
97
|
+
#
|
98
|
+
# This method prompts the user to enter an index number. It validates the input to ensure
|
99
|
+
# it is an integer within the range of 0 to (count - 1). If the input is invalid, an error
|
100
|
+
# message is displayed, and the user is prompted again.
|
101
|
+
#
|
102
|
+
# @param count [Integer] The count of items, setting the upper limit of the valid index range.
|
103
|
+
# @param error_message [String, nil] Custom error message for invalid index inputs.
|
104
|
+
# Defaults to the path 'question/error/index' if nil.
|
105
|
+
# @return [Integer] The user's input adjusted to be zero-based (input - 1) and validated to be within the range.
|
106
|
+
def read_index_input(count:, error_message: nil)
|
107
|
+
loop do
|
108
|
+
index = integer_answer(error_message: error_message) - 1
|
109
|
+
return index if index >= 0 && index < count
|
110
|
+
|
111
|
+
error = error_message || File.join('question', 'error', 'index')
|
112
|
+
puts Message.new(error)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mp-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcio F Paludo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
Helpers to facilitate scripts Writing.
|
@@ -21,17 +21,24 @@ extra_rdoc_files: []
|
|
21
21
|
files:
|
22
22
|
- lib/mp_utils.rb
|
23
23
|
- lib/resources/messages/hellow_world.txt
|
24
|
+
- lib/resources/messages/question/bool_sufix.txt
|
25
|
+
- lib/resources/messages/question/error/bool.txt
|
26
|
+
- lib/resources/messages/question/error/float.txt
|
27
|
+
- lib/resources/messages/question/error/index.txt
|
28
|
+
- lib/resources/messages/question/error/integer.txt
|
29
|
+
- lib/resources/messages/question/error/regex.txt
|
24
30
|
- lib/resources/path_helper.rb
|
31
|
+
- lib/utils/array.rb
|
25
32
|
- lib/utils/key.rb
|
26
33
|
- lib/utils/message.rb
|
34
|
+
- lib/utils/question.rb
|
27
35
|
homepage: https://github.com/MarcioFPaludo/ruby-mp-utils
|
28
36
|
licenses:
|
29
37
|
- MIT
|
30
38
|
metadata:
|
31
|
-
rubygems_mfa_required: 'true'
|
32
39
|
homepage_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
|
33
40
|
source_code_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
|
34
|
-
|
41
|
+
rubygems_mfa_required: 'true'
|
35
42
|
post_install_message:
|
36
43
|
rdoc_options: []
|
37
44
|
require_paths:
|