config_file_manager 0.1.3 → 0.1.4

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: e292b152343b7533c6dc061be052d078fb351e9506e54e093dd2070ae82db895
4
- data.tar.gz: f37d1f71cf8c1c71e844366832740ba472b0e7b65b0dd1dd11303419f03a84ee
3
+ metadata.gz: d77a5b859939e565703bc2dc5dce3dd9fe8d4cec4f33c7939585834d991a4e66
4
+ data.tar.gz: bd75f33acab4a3e6790bbf907d0409a0a24023f87f2a7f1f0236af26590525ca
5
5
  SHA512:
6
- metadata.gz: 31c41b7c8361f5ed1b29a4acc765f4052985585c2348cc91211cdfc0e83687955965b5bdb2158bfeff7f4d486207237981ab7fe0cdc8301b0f99eaf193af90aa
7
- data.tar.gz: da52f61bdee2f0c9d6ce90f2777a47ca92170591771359d1d1cc6262564a869d26016fee418da1455b123519e26f57bbfa7c0b1c644e2a742fab649c4e5de47e
6
+ metadata.gz: 3ad597c042ac18dcc1502113ad9ba87fb925c76a9ccb2e1e835bab8192a657d4a0ee2d1ce5ee46957db8c2115c0fc608a4a81ac5051ad1d06c7fed9296bb8ee7
7
+ data.tar.gz: 4bdcdb68a55c6f6166ef65b9ba6cfcb858288723894e219843616dbffd4c43f0da70b0bcc3735c5eff8f040430787f60e045e1223f6dda38af083e87e73d4060
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  inherit_gem:
2
- rubocop-espago: rubocop.yml
2
+ rubocop-espago: sorbet.yml
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 3.0
5
+ TargetRubyVersion: 3.3
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 4.0.6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## [0.1.4] - 2026-09-01
2
+
3
+ - Add recursive example directories
4
+ - Switch from YARD to RBS comments
5
+
1
6
  ## [0.1.3] - 2023-11-30
2
7
 
3
8
  - Add json parsing
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ConfigFileManager # rubocop:disable Style/StaticClass
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
@@ -1,3 +1,4 @@
1
+ # typed: true
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require 'fileutils'
@@ -14,29 +15,30 @@ class ConfigFileManager
14
15
 
15
16
  # Absolute path to the main directory that contains all config files (and subdirectories).
16
17
  #
17
- # @return [String]
18
+ #: String
18
19
  attr_reader :config_dir
19
20
 
20
21
  # Maximum depth of nested directories containing config files.
21
22
  #
22
- # @return [Integer]
23
+ #: Integer
23
24
  attr_reader :max_dir_depth
24
25
 
25
26
  # Current environment name. Used to load the correct section of YAML files.
26
27
  #
27
- # @return [String]
28
+ #: String
28
29
  attr_reader :env
29
30
 
30
31
  # Extension of the example/dummy version of a config file.
31
32
  # eg. `.example`, `.dummy`
32
33
  #
33
- # @return [String]
34
+ #: String
34
35
  attr_reader :example_extension
35
36
 
36
- # @param config_dir [String] Absolute path to the root config directory
37
- # @param example_extension [String]
38
- # @param max_dir_depth [Integer] Maximum depth of nested directories containing config files.
39
- # @param env [String] Current environment name
37
+ # @param config_dir Absolute path to the root config directory
38
+ # @param max_dir_depth Maximum depth of nested directories containing config files.
39
+ # @param env Current environment name
40
+ #
41
+ #: (String config_dir, ?example_extension: String, ?max_dir_depth: Integer, ?env: String) -> void
40
42
  def initialize(config_dir, example_extension: '.example', max_dir_depth: 5, env: 'development')
41
43
  @config_dir = config_dir
42
44
  @example_extension = example_extension
@@ -49,8 +51,9 @@ class ConfigFileManager
49
51
  # Returns an array of absolute paths to the found files
50
52
  # with the specified extension stripped away.
51
53
  #
52
- # @param example_extension [String] File extension of example files
53
- # @return [Array<String>]
54
+ # @param example_extension File extension of example files
55
+ #
56
+ #: (?example_extension: String, ?result: Array[String], ?depth: Integer, ?dir_path: String) -> Array[String]
54
57
  def files(example_extension: @example_extension, result: [], depth: 0, dir_path: @config_dir)
55
58
  return result if depth > @max_dir_depth
56
59
 
@@ -62,9 +65,9 @@ class ConfigFileManager
62
65
  # this essentially performs a depth limited search (DFS with a depth limit)
63
66
  next files(
64
67
  example_extension: example_extension,
65
- result: result,
66
- depth: depth + 1,
67
- dir_path: abs_path
68
+ result: result,
69
+ depth: depth + 1,
70
+ dir_path: abs_path,
68
71
  )
69
72
  end
70
73
 
@@ -76,8 +79,9 @@ class ConfigFileManager
76
79
  result
77
80
  end
78
81
 
79
- # @param example_extension [String]
80
- # @return [Array<String>] Absolute paths to missing config files.
82
+ # Absolute paths to missing config files.
83
+ #
84
+ #: (?example_extension: String) -> Array[String]
81
85
  def missing_files(example_extension: @example_extension)
82
86
  files(example_extension: example_extension).reject do |file|
83
87
  ::File.exist?(file)
@@ -86,9 +90,7 @@ class ConfigFileManager
86
90
 
87
91
  # Create the missing config files based on their dummy/example versions.
88
92
  #
89
- # @param example_extension [String]
90
- # @param print [Boolean]
91
- # @return [void]
93
+ #: (?example_extension: String, ?print: bool) -> void
92
94
  def create_missing_files(example_extension: @example_extension, print: false)
93
95
  puts COLORS.blue('== Copying missing config files ==') if print
94
96
  files(example_extension: example_extension).each do |file|
@@ -96,22 +98,44 @@ class ConfigFileManager
96
98
  end
97
99
  end
98
100
 
99
- # Search for directories under the `config_dir` directory
100
- # with the specified ending (eg. `.example`).
101
- # Returns an array of absolute paths to the found files
102
- # with the specified ending stripped away.
101
+ # Recursively search for directories under the `config_dir` directory
102
+ # with the specified extension (eg. `.example`).
103
+ # Returns an array of absolute paths to the found directories
104
+ # with the specified extension stripped away.
105
+ #
106
+ # @param example_extension ending of example directories
103
107
  #
104
- # @param example_extension [String] ending of example directories
105
- # @return [Array<String>]
106
- def dirs(example_extension: @example_extension)
107
- ::Dir.each_child(@config_dir)
108
- .map { ::File.join(@config_dir, _1) }
109
- .select { ::File.directory?(_1) && _1.end_with?(example_extension) }
110
- .map { _1.delete_suffix(example_extension) }
108
+ #: (?example_extension: String, ?result: Array[String], ?depth: Integer, ?dir_path: String) -> Array[String]
109
+ def dirs(example_extension: @example_extension, result: [], depth: 0, root_dir_path: @config_dir)
110
+ return result if depth > @max_dir_depth
111
+
112
+ ::Dir.each_child(root_dir_path) do |path|
113
+ abs_path = ::File.join(root_dir_path, path)
114
+
115
+ next unless ::File.directory?(abs_path)
116
+
117
+ # if the entry is a directory with the example extension, add it to the result array
118
+ if path.end_with?(example_extension)
119
+ result << abs_path.delete_suffix(example_extension)
120
+ next
121
+ end
122
+
123
+ # if the entry is a directory without the example extension, scan it recursively
124
+ # this essentially performs a depth limited search (DFS with a depth limit)
125
+ next dirs(
126
+ example_extension: example_extension,
127
+ result: result,
128
+ depth: depth + 1,
129
+ root_dir_path: abs_path,
130
+ )
131
+ end
132
+
133
+ result
111
134
  end
112
135
 
113
- # @param example_extension [String]
114
- # @return [Array<String>] Absolute paths to missing config directories.
136
+ # Absolute paths to missing config directories.
137
+ #
138
+ #: (?example_extension: String) -> Array[String]
115
139
  def missing_dirs(example_extension: @example_extension)
116
140
  dirs(example_extension: example_extension).reject do |file|
117
141
  ::Dir.exist?(file)
@@ -120,9 +144,7 @@ class ConfigFileManager
120
144
 
121
145
  # Create the missing config directories based on their dummy/example versions.
122
146
  #
123
- # @param example_extension [String]
124
- # @param print [Boolean]
125
- # @return [void]
147
+ #: (?example_extension: String, ?print: bool) -> void
126
148
  def create_missing_dirs(example_extension: @example_extension, print: false)
127
149
  puts COLORS.blue('== Copying missing config directories ==') if print
128
150
  dirs(example_extension: example_extension).each do |dir|
@@ -133,8 +155,7 @@ class ConfigFileManager
133
155
  # Converts a collection of absolute paths to an array of
134
156
  # relative paths.
135
157
  #
136
- # @param absolute_paths [Array<String>]
137
- # @return [Array<String>]
158
+ #: (Array[String] absolute_paths) -> Array[String]
138
159
  def to_relative_paths(absolute_paths)
139
160
  absolute_paths.map do |path|
140
161
  to_relative_path(path)
@@ -143,8 +164,7 @@ class ConfigFileManager
143
164
 
144
165
  # Converts an absolute path to a relative path
145
166
  #
146
- # @param absolute_path [String]
147
- # @return [String]
167
+ #: (String absolute_path) -> String
148
168
  def to_relative_path(absolute_path)
149
169
  absolute_path.delete_prefix("#{@config_dir}/")
150
170
  end
@@ -152,8 +172,7 @@ class ConfigFileManager
152
172
  # Converts a collection of relative paths to an array of
153
173
  # absolute paths.
154
174
  #
155
- # @param relative_paths [Array<String>]
156
- # @return [Array<String>]
175
+ #: (Array[String] relative_paths) -> Array[String]
157
176
  def to_absolute_paths(relative_paths)
158
177
  relative_paths.map do |path|
159
178
  to_absolute_path(path)
@@ -162,16 +181,14 @@ class ConfigFileManager
162
181
 
163
182
  # Converts a relative path to an absolute path.
164
183
  #
165
- # @param relative_path [String]
166
- # @return [String]
184
+ #: (String relative_path) -> String
167
185
  def to_absolute_path(relative_path)
168
186
  "#{@config_dir}/#{relative_path}"
169
187
  end
170
188
 
171
- # @param file_name [Array<String>]
172
- # @param env [String, nil]
173
- # @param symbolize [Boolean] Whether the keys should be converted to Ruby symbols
174
- # @return [Hash, Array]
189
+ # @param symbolize Whether the keys should be converted to Ruby symbols
190
+ #
191
+ #: (*String file_name, ?env: String?, ?symbolize: bool) -> untyped
175
192
  def load_yaml(*file_name, env: @env, symbolize: true)
176
193
  env = env.to_sym if env && symbolize
177
194
  parsed = ruby_load_yaml(load_erb(*file_name), symbolize_names: symbolize)
@@ -180,10 +197,9 @@ class ConfigFileManager
180
197
  parsed[env]
181
198
  end
182
199
 
183
- # @param file_name [Array<String>]
184
- # @param env [String, nil]
185
- # @param symbolize [Boolean] Whether the keys should be converted to Ruby symbols
186
- # @return [Hash, Array]
200
+ # @param symbolize Whether the keys should be converted to Ruby symbols
201
+ #
202
+ #: (*String file_name, ?env: String?, ?symbolize: bool) -> untyped
187
203
  def load_json(*file_name, env: @env, symbolize: true)
188
204
  env = env.to_sym if env && symbolize
189
205
  parsed = ::JSON.parse(load_erb(*file_name), symbolize_names: symbolize)
@@ -192,43 +208,39 @@ class ConfigFileManager
192
208
  parsed[env]
193
209
  end
194
210
 
195
- # @param file_name [Array<String>]
211
+ #: (*String file_name) -> void
196
212
  def delete_file(*file_name)
197
213
  ::File.delete(file_path(*file_name))
198
214
  end
199
215
 
200
- # @param dir_name [Array<String>]
216
+ #: (*String dir_name) -> void
201
217
  def delete_dir(*dir_name)
202
218
  ::FileUtils.rm_r(file_path(*dir_name))
203
219
  end
204
220
 
205
- # @param file_name [Array<String>]
206
- # @return [String]
221
+ #: (*String file_name) -> String
207
222
  def load_erb(*file_name)
208
223
  ::ERB.new(load_file(*file_name)).result
209
224
  end
210
225
 
211
- # @param file_name [Array<String>]
212
- # @return [String]
213
226
  # @raise [SystemCallError]
227
+ #
228
+ #: (*String file_name) -> String
214
229
  def load_file(*file_name)
215
230
  ::File.read file_path(*file_name)
216
231
  end
217
232
 
218
- # @param file_name [Array<String>]
219
- # @return [Boolean]
233
+ #: (*String file_name) -> bool
220
234
  def file_exist?(*file_name)
221
235
  ::File.exist? file_path(*file_name)
222
236
  end
223
237
 
224
- # @param dir_name [Array<String>]
225
- # @return [Boolean]
238
+ #: (*String dir_name) -> bool
226
239
  def dir_exist?(*dir_name)
227
240
  ::Dir.exist? file_path(*dir_name)
228
241
  end
229
242
 
230
- # @param file_name [Array<String>]
231
- # @return [String]
243
+ #: (*String file_name) -> String
232
244
  def file_path(*file_name)
233
245
  *path, name = file_name
234
246
  ::File.join(@config_dir, *path, name)
@@ -237,19 +249,22 @@ class ConfigFileManager
237
249
  private
238
250
 
239
251
  if ::Psych::VERSION >= '4'
240
- def ruby_load_yaml(content, **options) # rubocop:disable Style/DocumentationMethod
241
- ::YAML.load(content, aliases: true, **options) # rubocop:disable Security/YAMLLoad
252
+ # Load YAML content using the Psych 4+ API.
253
+ #
254
+ #: (String content, **untyped options) -> untyped
255
+ def ruby_load_yaml(content, **options)
256
+ ::YAML.load(content, aliases: true, **options)
242
257
  end
243
258
  else
244
- def ruby_load_yaml(content, **options) # rubocop:disable Style/DocumentationMethod
245
- ::YAML.load(content, **options) # rubocop:disable Security/YAMLLoad
259
+ # Load YAML content using the pre Psych 4 API.
260
+ #
261
+ #: (String content, **untyped options) -> untyped
262
+ def ruby_load_yaml(content, **options)
263
+ ::YAML.load(content, **options)
246
264
  end
247
265
  end
248
266
 
249
- # @param original_name [String]
250
- # @param new_name [String]
251
- # @param print [Boolean]
252
- # @return [Boolean]
267
+ #: (String original_name, String new_name, ?print: bool) -> bool
253
268
  def create_missing_file(original_name, new_name, print: false)
254
269
  return false if ::File.exist?(new_name)
255
270
 
@@ -262,10 +277,7 @@ class ConfigFileManager
262
277
  true
263
278
  end
264
279
 
265
- # @param original_name [String]
266
- # @param new_name [String]
267
- # @param print [Boolean]
268
- # @return [Boolean]
280
+ #: (String original_name, String new_name, ?print: bool) -> bool
269
281
  def create_missing_dir(original_name, new_name, print: false)
270
282
  return false if ::Dir.exist?(new_name)
271
283
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_file_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-11-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: pastel
@@ -32,13 +31,13 @@ extensions: []
32
31
  extra_rdoc_files: []
33
32
  files:
34
33
  - ".rubocop.yml"
34
+ - ".ruby-version"
35
35
  - CHANGELOG.md
36
36
  - LICENSE.txt
37
37
  - README.md
38
38
  - Rakefile
39
39
  - lib/config_file_manager.rb
40
40
  - lib/config_file_manager/version.rb
41
- - sig/config_loader.rbs
42
41
  homepage: https://github.com/Verseth/ruby-config-loader
43
42
  licenses:
44
43
  - MIT
@@ -46,7 +45,6 @@ metadata:
46
45
  homepage_uri: https://github.com/Verseth/ruby-config-loader
47
46
  source_code_uri: https://github.com/Verseth/ruby-config-loader
48
47
  rubygems_mfa_required: 'true'
49
- post_install_message:
50
48
  rdoc_options: []
51
49
  require_paths:
52
50
  - lib
@@ -54,15 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
52
  requirements:
55
53
  - - ">="
56
54
  - !ruby/object:Gem::Version
57
- version: 3.0.0
55
+ version: 3.3.0
58
56
  required_rubygems_version: !ruby/object:Gem::Requirement
59
57
  requirements:
60
58
  - - ">="
61
59
  - !ruby/object:Gem::Version
62
60
  version: '0'
63
61
  requirements: []
64
- rubygems_version: 3.4.21
65
- signing_key:
62
+ rubygems_version: 4.0.16
66
63
  specification_version: 4
67
64
  summary: Gem that makes it easy to load config files.
68
65
  test_files: []
@@ -1,4 +0,0 @@
1
- module ConfigFileManager
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end