mp-utils 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 472fb772fc669be45aee52e5f931823934e59cb5f36974536dbfea782024ac9d
4
- data.tar.gz: 9c5079304a471c97261ec621457575b1b79c9454a8a60f7d19ee9b0a51a7c777
3
+ metadata.gz: bd4e848d53092d07f0ffe66865c1dd718fec86eac82143a3c487e7163755d86a
4
+ data.tar.gz: cd14a3a2876dd20e67591a3da7f1da71ffb79ce6267647a6453884e47724b87e
5
5
  SHA512:
6
- metadata.gz: 79864c2f162bb4d136ae0762d4196c868e0ce4136a31bb873ce79289478734f442acfc18e882d942b53b15e26c570a8b4d18c144f88d1ceae4573c01121583d8
7
- data.tar.gz: cd1ed6a8fdb3c724d1953c0df5107468936b5a4800b537097ca32d3f134123c93f39c45ee327a347037e6246fe23600030128fe12f7371034d11bca9fb347204
6
+ metadata.gz: c43bbb206cd8778bfef333f1ac572b8d1cb5daadf2bda9c179849aaf8d39e2b77fb7507a165e57308e2b78e6ef8c77bb3025a502011a11a4f32b81a9ed88298c
7
+ data.tar.gz: 4dd356d68a6304642c639ecc8f82a6e5da7583876d507e726425fcec4fa80cf5b062047412aac2637e07b82a2d0dc6c1678332465eb377f10243a29d779ab9c2
data/lib/mp_utils.rb CHANGED
@@ -7,4 +7,5 @@ require_relative 'utils/message'
7
7
  require_relative 'utils/question'
8
8
  require_relative 'utils/version_manager'
9
9
  require_relative 'utils/ansi_style_manager'
10
+ require_relative 'utils/directory_structure'
10
11
  require_relative 'resources/path_helper'
@@ -0,0 +1,215 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ # Class to represent and process a directory structure.
6
+ # It can be initialized with a hash or a YAML file.
7
+ #
8
+ # ## Using HASH
9
+ #
10
+ # The following example shows how to use DirectoryStructure with a hash.
11
+ # ```ruby
12
+ # hash = {
13
+ # 'lib' => {
14
+ # 'version.rb' => nil,
15
+ # 'source' => [
16
+ # 'file_1.rb',
17
+ # 'file_2.rb',
18
+ # 'file_3.rb'
19
+ # ],
20
+ # 'resource' => {
21
+ # 'images' => [
22
+ # 'potato.png',
23
+ # 'fries.jpeg',
24
+ # 'franch_fries.png'
25
+ # ],
26
+ # 'scripts' => {
27
+ # validation: 'teste.rb',
28
+ # sorting: [
29
+ # 'shellsort.rb',
30
+ # 'quicksort.rb'
31
+ # ],
32
+ # 'build.rb' => nil
33
+ # }
34
+ # },
35
+ # generated: 'file.generated.rb'
36
+ # }
37
+ # }
38
+ # directory_structure = DirectoryStructure.new(hash)
39
+ # puts "That is my directory structure:\n#{directory_structure}"
40
+ # # Output:
41
+ # # That is my directory structure:
42
+ # # lib
43
+ # # ╠═ version.rb
44
+ # # ╠═ source
45
+ # # ║ ╠═ file_1.rb
46
+ # # ║ ╠═ file_2.rb
47
+ # # ║ ╚═ file_3.rb
48
+ # # ╠═ resource
49
+ # # ║ ╠═ images
50
+ # # ║ ║ ╠═ potato.png
51
+ # # ║ ║ ╠═ fries.jpeg
52
+ # # ║ ║ ╚═ franch_fries.png
53
+ # # ║ ╚═ scripts
54
+ # # ║ ╠═ validation
55
+ # # ║ ║ ╚═ teste.rb
56
+ # # ║ ╠═ sorting
57
+ # # ║ ║ ╠═ shellsort.rb
58
+ # # ║ ║ ╚═ quicksort.rb
59
+ # # ║ ╚═ build.rb
60
+ # # ╚═ generated
61
+ # # ╚═ file.generated.rb
62
+ # ```
63
+ #
64
+ # ## Using a YAML/YML File
65
+ #
66
+ # First, you need to create a yml file.
67
+ #
68
+ # Example:
69
+ # ```yml
70
+ # lib:
71
+ # version.rb:
72
+ # source:
73
+ # - file_1.rb
74
+ # - file_2.rb
75
+ # - file_3.rb
76
+ # resource:
77
+ # images:
78
+ # - potato.png
79
+ # - fries.jpeg
80
+ # - franch_fries.png
81
+ # scripts:
82
+ # validation: teste.rb
83
+ # sorting:
84
+ # - shellsort.rb
85
+ # - quicksort.rb
86
+ # build.rb:
87
+ # generated: file.generated.rb
88
+ # ```
89
+ #
90
+ # Considering that the file path is passed as a reference.
91
+ #
92
+ # You can initialize the class as in the example below:
93
+ #
94
+ # ```ruby
95
+ # path = 'Replace/By/Your/YAML/FILE/PATH'
96
+ # directory_structure = DirectoryStructure.new(path)
97
+ # puts "That is my directory structure:\n#{directory_structure}"
98
+ # # Output:
99
+ # # That is my directory structure:
100
+ # # lib
101
+ # # ╠═ version.rb
102
+ # # ╠═ source
103
+ # # ║ ╠═ file_1.rb
104
+ # # ║ ╠═ file_2.rb
105
+ # # ║ ╚═ file_3.rb
106
+ # # ╠═ resource
107
+ # # ║ ╠═ images
108
+ # # ║ ║ ╠═ potato.png
109
+ # # ║ ║ ╠═ fries.jpeg
110
+ # # ║ ║ ╚═ franch_fries.png
111
+ # # ║ ╚═ scripts
112
+ # # ║ ╠═ validation
113
+ # # ║ ║ ╚═ teste.rb
114
+ # # ║ ╠═ sorting
115
+ # # ║ ║ ╠═ shellsort.rb
116
+ # # ║ ║ ╚═ quicksort.rb
117
+ # # ║ ╚═ build.rb
118
+ # # ╚═ generated
119
+ # # ╚═ file.generated.rb
120
+ # ```
121
+ class DirectoryStructure
122
+ # Initializes the DirectoryStructure object.
123
+ #
124
+ # @param content [Hash, String] A hash representing the directory structure or a path to a YAML file.
125
+ # @raise [RuntimeError] If the content is not a Hash or a valid YAML file path.
126
+ def initialize(content)
127
+ if content.is_a?(Hash)
128
+ @dir_hash = content
129
+ elsif File.exist?(content)
130
+ @dir_hash = YAML.load_file(content)
131
+ else
132
+ raise 'Need be initialized with a Hash or yaml file path'
133
+ end
134
+ end
135
+
136
+ # Converts the directory structure to a string representation.
137
+ #
138
+ # @return [String] The string representation of the directory structure.
139
+ def to_s
140
+ @output = String.new('')
141
+ process_node(@dir_hash)
142
+ @output
143
+ end
144
+
145
+ private
146
+
147
+ # Generates the indentation string based on the key.
148
+ #
149
+ # @param key [Symbol] The key indicating the type of indentation.
150
+ # @return [String] The indentation string.
151
+ def generate_indent(key)
152
+ { middle: '║ ', last: ' ' }[key] || ''
153
+ end
154
+
155
+ # Concatenates the node to the output string with the given prefix and indentation.
156
+ #
157
+ # @param node [String] The node to be concatenated.
158
+ # @param prefix [Symbol] The prefix indicating the type of node.
159
+ # @param indent [Array<Symbol>] The array of indentation keys.
160
+ # @return [void]
161
+ def concant(node, prefix:, indent:)
162
+ prefix_string = { middle: '╠═ ', last: '╚═ ' }[prefix] || ''
163
+ indent_string = indent.map { |i| generate_indent(i) }.join
164
+ @output << "#{indent_string}#{prefix_string}#{node}\n"
165
+ end
166
+
167
+ # Processes an array of nodes and concatenates them to the output string.
168
+ #
169
+ # @param array [Array] The array of nodes to be processed.
170
+ # @param indent [Array<Symbol>] The array of indentation keys.
171
+ # @return [void]
172
+ def process_array(array, indent:)
173
+ array.each_with_index do |item, index|
174
+ prefix = index == array.size - 1 ? :last : :middle
175
+ concant(item, prefix: prefix, indent: indent)
176
+ end
177
+ end
178
+
179
+ # Processes a hash of nodes and concatenates them to the output string.
180
+ #
181
+ # @param hash [Hash] The hash of nodes to be processed.
182
+ # @param level [Integer] The current level of indentation.
183
+ # @param indent [Array<Symbol>] The array of indentation keys.
184
+ # @return [void]
185
+ def process_hash(hash, level:, indent:)
186
+ hash.each_with_index do |(key, value), index|
187
+ new_indent = indent.dup
188
+ prefix = :empty
189
+
190
+ if level > new_indent.size
191
+ prefix = index == hash.size - 1 ? :last : :middle
192
+ new_indent = indent + [prefix]
193
+ end
194
+
195
+ concant(key, prefix: prefix, indent: indent)
196
+ process_node(value, indent: new_indent, level: level)
197
+ end
198
+ end
199
+
200
+ # Processes a node (hash, array, or other) and concatenates it to the output string.
201
+ #
202
+ # @param node [Object] The node to be processed.
203
+ # @param level [Integer] The current level of indentation.
204
+ # @param indent [Array<Symbol>] The array of indentation keys.
205
+ # @return [void]
206
+ def process_node(node, level: 0, indent: [''])
207
+ if node.is_a?(Hash)
208
+ process_hash(node, indent: indent, level: level + 1)
209
+ elsif node.is_a?(Array)
210
+ process_array(node, indent: indent)
211
+ elsif !node.nil?
212
+ concant(node, prefix: :last, indent: indent)
213
+ end
214
+ end
215
+ 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.3.1
4
+ version: 0.4.0
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-07-02 00:00:00.000000000 Z
11
+ date: 2024-07-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Helpers to facilitate scripts Writing.
@@ -32,6 +32,7 @@ files:
32
32
  - lib/utils/ansi_style_manager.rb
33
33
  - lib/utils/array.rb
34
34
  - lib/utils/constants.rb
35
+ - lib/utils/directory_structure.rb
35
36
  - lib/utils/key.rb
36
37
  - lib/utils/message.rb
37
38
  - lib/utils/question.rb
@@ -42,6 +43,7 @@ licenses:
42
43
  metadata:
43
44
  homepage_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
44
45
  source_code_uri: https://github.com/MarcioFPaludo/ruby-mp-utils
46
+ documentation_uri: https://marciofpaludo.github.io/ruby-mp-utils
45
47
  rubygems_mfa_required: 'true'
46
48
  post_install_message:
47
49
  rdoc_options: []