mp-utils 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/mp_utils.rb +5 -0
- data/lib/resources/messages/hellow_world.txt +1 -0
- data/lib/resources/path_helper.rb +36 -0
- data/lib/utils/key.rb +77 -0
- data/lib/utils/message.rb +122 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e35a3d445a73ca931a0503b2d162c1a54a40b8b3bc04f4961884c607a6c6dfde
|
4
|
+
data.tar.gz: 510838aa718f0475e7ad846b0913325b89a711050566ee2778a655e29ec2fdc4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a3e48fb89f3505cc381529c052e26fbd61ed490674216fdd8ffe121f8849ce931e0432ac8d02b0ab88e3ce5353ab7424ff656069057575f64e3793636d3ba854
|
7
|
+
data.tar.gz: de22e7695b09793b51aaa80205b036c4b2e2e7adba46a2c41c29543cfe6798795ffc8e93bbd037b43757472a294da817fe2c4b478e61d541333980623dc99452
|
data/lib/mp_utils.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Hellow World from MPUtils!
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The Resources module manages and provides access to file system paths used by a script or application.
|
4
|
+
# It facilitates the definition and retrieval of a default library path, alongside a customizable path
|
5
|
+
# that can be set at runtime either programmatically using the {define} method or through the environment
|
6
|
+
# variable "SCRIPT_CUSTOM_RESOURCES". This flexibility allows applications to dynamically access resources
|
7
|
+
# stored in various locations, depending on execution environment or user-defined settings.
|
8
|
+
module Resources
|
9
|
+
# Retrieves the path to the directory containing this module, which serves as the default library path.
|
10
|
+
#
|
11
|
+
# @return [String] the directory path of this module, used as the default library path.
|
12
|
+
def self.library_path
|
13
|
+
__dir__
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the custom path for resources as defined by the user. This path can be set at runtime
|
17
|
+
# through the use of the environment variable "SCRIPT_CUSTOM_RESOURCES" or programmatically via the
|
18
|
+
# {define} method. This method allows for easy retrieval of the custom path, facilitating flexible
|
19
|
+
# resource management.
|
20
|
+
#
|
21
|
+
# @return [String, nil] the custom resources path as defined by the environment variable, or nil if it hasn't been set.
|
22
|
+
def self.custom_path
|
23
|
+
ENV['SCRIPT_CUSTOM_RESOURCES']
|
24
|
+
end
|
25
|
+
|
26
|
+
# Provides a means to programmatically define a custom resources path at runtime. This method updates
|
27
|
+
# the "SCRIPT_CUSTOM_RESOURCES" environment variable to store the custom path, making it retrievable
|
28
|
+
# through the {custom_path} method. This allows for dynamic setting of resource paths based on runtime
|
29
|
+
# conditions or user preferences.
|
30
|
+
#
|
31
|
+
# @param custom_path [String] the custom path to be set for resource access.
|
32
|
+
# @return [void]
|
33
|
+
def self.define(custom_path:)
|
34
|
+
ENV['SCRIPT_CUSTOM_RESOURCES'] = custom_path
|
35
|
+
end
|
36
|
+
end
|
data/lib/utils/key.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The Key class is designed to encapsulate strings within a specific prefix and suffix,
|
4
|
+
# allowing for easy identification and manipulation of placeholders within messages.
|
5
|
+
# This can be particularly useful in templating systems where placeholders need
|
6
|
+
# to be dynamically replaced with actual content.
|
7
|
+
#
|
8
|
+
# @example Creating a new Key and converting it to a string
|
9
|
+
# key = Key.new("username")
|
10
|
+
# key.to_s # => "<||username||>"
|
11
|
+
#
|
12
|
+
# @example Finding keys within a string
|
13
|
+
# keys = Key.find_keys_in("Hello, <||username||>! Your code is <||code||>.")
|
14
|
+
# keys.map(&:to_s) # => ["<||username||>", "<||code||>"]
|
15
|
+
class Key
|
16
|
+
# @!attribute [r] value
|
17
|
+
# @return [String] the value of the key without the prefix and suffix.
|
18
|
+
attr_reader :value
|
19
|
+
|
20
|
+
# Initializes a new Key with the given value.
|
21
|
+
#
|
22
|
+
# @param value [#to_s] the value to be encapsulated by the Key.
|
23
|
+
def initialize(value)
|
24
|
+
@value = value.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
# Checks equality of two Key objects based on their value.
|
28
|
+
#
|
29
|
+
# @param other [Key] the other Key object to compare with.
|
30
|
+
# @return [Boolean] true if both Keys have the same value, false otherwise.
|
31
|
+
def ==(other)
|
32
|
+
self.class == other.class && @value == other.value
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the string representation of the Key, including its prefix and suffix.
|
36
|
+
#
|
37
|
+
# @return [String] the string representation of the Key.
|
38
|
+
def to_s
|
39
|
+
"#{Key.prefix}#{@value}#{Key.suffix}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the escaped Regexp representation of the Key.to_s return.
|
43
|
+
#
|
44
|
+
# @return [Regexp] the escaped regexp representation of the Key.to_s return.
|
45
|
+
def to_regexp
|
46
|
+
/#{Regexp.escape(to_s)}/
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Static Methods
|
51
|
+
class Key
|
52
|
+
# Finds and returns all Key instances within the given string.
|
53
|
+
#
|
54
|
+
# @param value [#to_s] the string to search for keys.
|
55
|
+
# @return [Array<Key>] an array of Key instances found within the given string.
|
56
|
+
def self.find_keys_in(value)
|
57
|
+
ep = Regexp.escape(prefix)
|
58
|
+
es = Regexp.escape(suffix)
|
59
|
+
value.to_s.scan(/#{ep}[^#{ep}#{es}]+#{es}/).map do |key|
|
60
|
+
Key.new(key.gsub(/(#{ep})|(#{es})/, ''))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns the prefix used to identify the start of a Key in a string.
|
65
|
+
#
|
66
|
+
# @return [String] the prefix.
|
67
|
+
def self.prefix
|
68
|
+
'<||'
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns the suffix used to identify the end of a Key in a string.
|
72
|
+
#
|
73
|
+
# @return [String] the suffix.
|
74
|
+
def self.suffix
|
75
|
+
'||>'
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative File.join('..', 'resources', 'path_helper')
|
4
|
+
require_relative 'key'
|
5
|
+
|
6
|
+
# The Message class represents a mechanism for dynamically handling and formatting messages.
|
7
|
+
# It supports the substitution of placeholders within a message template with actual data.
|
8
|
+
# The class leverages file-based message templates, allowing for easy localization or customization
|
9
|
+
# of messages. It integrates seamlessly with the Resources module to access these templates from
|
10
|
+
# a customizable path set via the "SCRIPT_CUSTOM_RESOURCES" environment variable or from a default
|
11
|
+
# library path.
|
12
|
+
#
|
13
|
+
# @example Creating a new Message instance and formatting it
|
14
|
+
# # Assuming "hellow_world" is a file that says "Hello, world!"
|
15
|
+
# message = Message.new("hellow_world")
|
16
|
+
# puts message.to_s # => "Hello, world!"
|
17
|
+
class Message
|
18
|
+
attr_reader :message
|
19
|
+
|
20
|
+
# Initializes a new instance of the Message class with a given message template.
|
21
|
+
#
|
22
|
+
# @param message [String] The message template to be used.
|
23
|
+
# @param replaces [String] The replaces Hash used to change key finded in @message by custom values.
|
24
|
+
def initialize(message, replaces: nil)
|
25
|
+
raise 'Messsage replaces content need be a Hash' if !replaces.nil? && !replaces.is_a?(Hash)
|
26
|
+
@replaces = replaces
|
27
|
+
@message = message
|
28
|
+
end
|
29
|
+
|
30
|
+
# Compares two Message instances for equality based on their message content.
|
31
|
+
#
|
32
|
+
# @param other [Message] The other Message instance to compare with.
|
33
|
+
# @return [Boolean] True if the messages are equal, otherwise false.
|
34
|
+
def ==(other)
|
35
|
+
self.class == other.class && @message == other.message
|
36
|
+
end
|
37
|
+
|
38
|
+
# Converts the message template into a string, replacing any placeholders with actual data.
|
39
|
+
# This method searches for keys within the message and replaces them with corresponding
|
40
|
+
# content from message files located in either the custom path or the library path and appling
|
41
|
+
# the given replaces.
|
42
|
+
#
|
43
|
+
# @return [String] The formatted message with placeholders substituted with actual content.
|
44
|
+
def to_s
|
45
|
+
new_message = String.new(@message)
|
46
|
+
keys = Key.find_keys_in(@message)
|
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
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Private Methods
|
72
|
+
class Message
|
73
|
+
private
|
74
|
+
|
75
|
+
# Determines the custom path for message files, if set through the "SCRIPT_CUSTOM_RESOURCES" environment variable.
|
76
|
+
#
|
77
|
+
# @return [String, nil] The custom path for message files, or nil if not set.
|
78
|
+
def custom_path
|
79
|
+
path = Resources.custom_path
|
80
|
+
return nil if path.nil?
|
81
|
+
|
82
|
+
File.join(path, 'messages')
|
83
|
+
end
|
84
|
+
|
85
|
+
# Provides the default library path for message files. This path is used as a fallback
|
86
|
+
# when a message file is not found in the custom path.
|
87
|
+
#
|
88
|
+
# @return [String] The path to the default library of message files.
|
89
|
+
def library_path
|
90
|
+
File.join(Resources.library_path, 'messages')
|
91
|
+
end
|
92
|
+
|
93
|
+
# Attempts to recover and return the content of a message file identified by the file_name parameter.
|
94
|
+
# It first looks in the custom path (if defined) and then in the library path.
|
95
|
+
#
|
96
|
+
# @param file_name [String] The name of the file containing the message content to be recovered.
|
97
|
+
# @return [String] The content of the message file, or the file_name itself if the file cannot be found.
|
98
|
+
def recover_message_with(file_name)
|
99
|
+
path = custom_path
|
100
|
+
unless path.nil?
|
101
|
+
message = recover_content_with(path, file_name)
|
102
|
+
return message unless message.nil?
|
103
|
+
end
|
104
|
+
|
105
|
+
message = recover_content_with(library_path, file_name)
|
106
|
+
return message unless message.nil?
|
107
|
+
|
108
|
+
file_name
|
109
|
+
end
|
110
|
+
|
111
|
+
# Reads and returns the content of a message file located at a specific path.
|
112
|
+
#
|
113
|
+
# @param path [String] The path where the message file is located.
|
114
|
+
# @param file_name [String] The name of the file to be read.
|
115
|
+
# @return [String, nil] The content of the message file, or nil if the file does not exist.
|
116
|
+
def recover_content_with(path, file_name)
|
117
|
+
file_path = File.join(path, "#{file_name}.txt")
|
118
|
+
return nil unless File.exist?(file_path)
|
119
|
+
|
120
|
+
File.read(file_path)
|
121
|
+
end
|
122
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mp-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcio F Paludo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
Helpers to facilitate scripts Writing.
|
15
|
+
It can centralize messages in files and also add facilitators for the recovery and manipulation of some contents.
|
16
|
+
email:
|
17
|
+
- marciof.paludo@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/mp_utils.rb
|
23
|
+
- lib/resources/messages/hellow_world.txt
|
24
|
+
- lib/resources/path_helper.rb
|
25
|
+
- lib/utils/key.rb
|
26
|
+
- lib/utils/message.rb
|
27
|
+
homepage: https://github.com/MarcioFPaludo/ruby-mp-utils
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
rubygems_mfa_required: 'true'
|
32
|
+
homepage_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
|
33
|
+
source_code_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
|
34
|
+
changelog_uri: https://github.com/MarcioFPaludo/ruby-mp-utils/blob/main/CHANGELOG.md
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.6.10
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.4.10
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: The MP-Utils library aims to facilitate the writing of daily scripts
|
54
|
+
test_files: []
|