zeitwerk 2.6.9 → 2.6.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1de024fb1ddb2f4bc64ced5aacf898f01b6336d5d8b85701d0757e256d062fb
4
- data.tar.gz: d3f06cb22aad419d5c1778fc4a13e1cc036cd062b5ac1fcece5eb4fa3ba1ce00
3
+ metadata.gz: 2b2955613976d014a322168f1976a93b29e388b4c72ecf5acdbb0186d212fe7c
4
+ data.tar.gz: 57df7539d6fa37fcf41f1d37b6c2fa0a8db5af7af02333da35566fca92a03d8a
5
5
  SHA512:
6
- metadata.gz: 3b49724afa7c8097eb6014264faa50f6320c7686760fc5677f6c648f6f4c482b2aa6efb8f78621721c6b00ed424a0a77864cdf8fafb2a481a0ddd1c333115d04
7
- data.tar.gz: 01b7782fea2affe16c5241f9325649e90f3beb3150f168cecf6d710fdfffb66a9bfa853ca4ce562ddbdca3133718b7b2bdb2b1260cf997724d668d6639a5199b
6
+ metadata.gz: 625ef1f9a15205afa2efea139f63b5b800660d57631fda75deccd172f72e82fb6454efe7465edb8024a9a8536b463fa523ead1582dc13aac54dfddb5535e31a9
7
+ data.tar.gz: d0e0b1ff25b25ce5b9d3223fa01b3f04b4eefa7e316d55bad95686265f194aee24599e146c1ef9a05dd20dc16f670733e17e494ccd469ea9aef7e4a8b2fb973c
data/README.md CHANGED
@@ -1296,7 +1296,7 @@ If the argument corresponds to an [ignored file or directory](#ignoring-parts-of
1296
1296
  loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1297
1297
  ```
1298
1298
 
1299
- `Zeitwer::NameError` is raised if a constant path cannot be derived from it:
1299
+ `Zeitwerk::NameError` is raised if a constant path cannot be derived from it:
1300
1300
 
1301
1301
  ```ruby
1302
1302
  loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
@@ -140,4 +140,48 @@ module Zeitwerk::Loader::Helpers
140
140
  private def crem(parent, cname)
141
141
  parent.__send__(:remove_const, cname)
142
142
  end
143
+
144
+ CNAME_VALIDATOR = Module.new
145
+ private_constant :CNAME_VALIDATOR
146
+
147
+ # @raise [Zeitwerk::NameError]
148
+ # @sig (String, String) -> Symbol
149
+ private def cname_for(basename, abspath)
150
+ cname = inflector.camelize(basename, abspath)
151
+
152
+ unless cname.is_a?(String)
153
+ raise TypeError, "#{inflector.class}#camelize must return a String, received #{cname.inspect}"
154
+ end
155
+
156
+ if cname.include?("::")
157
+ raise Zeitwerk::NameError.new(<<~MESSAGE, cname)
158
+ wrong constant name #{cname} inferred by #{inflector.class} from
159
+
160
+ #{abspath}
161
+
162
+ #{inflector.class}#camelize should return a simple constant name without "::"
163
+ MESSAGE
164
+ end
165
+
166
+ begin
167
+ CNAME_VALIDATOR.const_defined?(cname, false)
168
+ rescue ::NameError => error
169
+ path_type = ruby?(abspath) ? "file" : "directory"
170
+
171
+ raise Zeitwerk::NameError.new(<<~MESSAGE, error.name)
172
+ #{error.message} inferred by #{inflector.class} from #{path_type}
173
+
174
+ #{abspath}
175
+
176
+ Possible ways to address this:
177
+
178
+ * Tell Zeitwerk to ignore this particular #{path_type}.
179
+ * Tell Zeitwerk to ignore one of its parent directories.
180
+ * Rename the #{path_type} to comply with the naming conventions.
181
+ * Modify the inflector to handle this case.
182
+ MESSAGE
183
+ end
184
+
185
+ cname.to_sym
186
+ end
143
187
  end
@@ -237,15 +237,13 @@ module Zeitwerk
237
237
  return unless dir?(abspath) || ruby?(abspath)
238
238
  return if ignored_path?(abspath)
239
239
 
240
- cnames = []
241
- abspaths = []
240
+ paths = []
242
241
 
243
242
  if ruby?(abspath)
244
243
  basename = File.basename(abspath, ".rb")
245
244
  return if hidden?(basename)
246
245
 
247
- cnames << inflector.camelize(basename, abspath).to_sym
248
- abspaths << abspath
246
+ paths << [basename, abspath]
249
247
  walk_up_from = File.dirname(abspath)
250
248
  else
251
249
  walk_up_from = abspath
@@ -260,28 +258,15 @@ module Zeitwerk
260
258
  basename = File.basename(dir)
261
259
  return if hidden?(basename)
262
260
 
263
- unless collapse?(dir)
264
- cnames << inflector.camelize(basename, dir).to_sym
265
- abspaths << dir
266
- end
261
+ paths << [basename, abspath] unless collapse?(dir)
267
262
  end
268
263
 
269
264
  return unless root_namespace
270
265
 
271
- if cnames.empty?
266
+ if paths.empty?
272
267
  real_mod_name(root_namespace)
273
268
  else
274
- # We reverse before validating the segments to report the leftmost
275
- # problematic one, if any.
276
- cnames.reverse!
277
-
278
- cname_validator = Module.new
279
- cnames.each_with_index do |cname, i|
280
- cname_validator.const_defined?(cname, false)
281
- rescue ::NameError
282
- j = -(i + 1)
283
- raise Zeitwerk::NameError.new("cannot derive a constant name from #{abspaths[j]}")
284
- end
269
+ cnames = paths.reverse_each.map { |b, a| cname_for(b, a) }
285
270
 
286
271
  if root_namespace == Object
287
272
  cnames.join("::")
@@ -422,34 +407,15 @@ module Zeitwerk
422
407
  # @sig (String, Module) -> void
423
408
  private def set_autoloads_in_dir(dir, parent)
424
409
  ls(dir) do |basename, abspath|
425
- begin
426
- if ruby?(basename)
427
- basename.delete_suffix!(".rb")
428
- cname = inflector.camelize(basename, abspath).to_sym
429
- autoload_file(parent, cname, abspath)
410
+ if ruby?(basename)
411
+ basename.delete_suffix!(".rb")
412
+ autoload_file(parent, cname_for(basename, abspath), abspath)
413
+ else
414
+ if collapse?(abspath)
415
+ set_autoloads_in_dir(abspath, parent)
430
416
  else
431
- if collapse?(abspath)
432
- set_autoloads_in_dir(abspath, parent)
433
- else
434
- cname = inflector.camelize(basename, abspath).to_sym
435
- autoload_subdir(parent, cname, abspath)
436
- end
417
+ autoload_subdir(parent, cname_for(basename, abspath), abspath)
437
418
  end
438
- rescue ::NameError => error
439
- path_type = ruby?(abspath) ? "file" : "directory"
440
-
441
- raise NameError.new(<<~MESSAGE, error.name)
442
- #{error.message} inferred by #{inflector.class} from #{path_type}
443
-
444
- #{abspath}
445
-
446
- Possible ways to address this:
447
-
448
- * Tell Zeitwerk to ignore this particular #{path_type}.
449
- * Tell Zeitwerk to ignore one of its parent directories.
450
- * Rename the #{path_type} to comply with the naming conventions.
451
- * Modify the inflector to handle this case.
452
- MESSAGE
453
419
  end
454
420
  end
455
421
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.6.9"
4
+ VERSION = "2.6.10"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeitwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.9
4
+ version: 2.6.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-25 00:00:00.000000000 Z
11
+ date: 2023-07-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem