zeitwerk 2.8.1 → 2.8.2

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: f6f3971296735d2ad5ae2d2621b5f26d6f480e7b69fde7e6eb21679a2b0c795c
4
- data.tar.gz: 41ce43ff32412f63d21fb5bd9f45dc070e48e96f03be1e49eb2a5c6c5ef107ba
3
+ metadata.gz: bc67ff7496feaaf2e3598809e67733b9be0f92f69820736e3471ae0487c39d85
4
+ data.tar.gz: cecf1045923045d9d11d4f5e6c214a9e6dd14abd742fc1065c6f23a8aec82e13
5
5
  SHA512:
6
- metadata.gz: 2b543e2a0db16c3dcdf8318faeaae7e1a72b0001b6116fa0081fa991266fbcc75da8ebdecaf9cf17568d9b169688810f411fb5d578a6ce7f5fad60a5fea37380
7
- data.tar.gz: 021343c474a12a015071b20eaff22c53fdf9d0a49e606ee460be809fd731be065e72573ffb082bfab19b14f432000b309cc3e8e44e75669b97d91fa5b275519a
6
+ metadata.gz: 739fe00bce3542defa99e3c7a9014d7c4e00d0aa5a062e996c00dc2c9139e26de055e5aee806807c6da614504275bf2c790904d323d47213c031c291713daad6
7
+ data.tar.gz: 85acfebcb1de71e4c54e94803fb3be1c804960d3ba3754cdb6903a32a61e52f139cc06f7672f2cdcabb189dc9d66975479fc20a6733152e58e3131797fc151e4
@@ -7,6 +7,9 @@ module Zeitwerk::Loader::Config
7
7
  extend Zeitwerk::Internal
8
8
  include Zeitwerk::RealModName
9
9
 
10
+ UNDEFINED = Object.new.freeze
11
+ private_constant :UNDEFINED
12
+
10
13
  #: camelize(String, String) -> String
11
14
  attr_accessor :inflector
12
15
 
@@ -276,12 +279,19 @@ module Zeitwerk::Loader::Config
276
279
  # # ...
277
280
  # end
278
281
  #
279
- #: (String?) { (top, String) -> void } -> void ! TypeError
280
- def on_load(cpath = :ANY, &block)
281
- raise TypeError, 'on_load only accepts strings' unless cpath.is_a?(String) || cpath == :ANY
282
+ #: (String) { (top, String) -> void } -> void ! TypeError | NameError
283
+ #| { (String, top, String) -> void } -> void
284
+ def on_load(cpath = UNDEFINED, &block)
285
+ key = if cpath.equal?(UNDEFINED)
286
+ :ANY
287
+ elsif !cpath.is_a?(String)
288
+ raise TypeError, 'on_load only accepts strings'
289
+ else
290
+ @cpv.validate!(cpath)
291
+ end
282
292
 
283
293
  mutex.synchronize do
284
- (on_load_callbacks[cpath] ||= []) << block
294
+ (on_load_callbacks[key] ||= []) << block
285
295
  end
286
296
  end
287
297
 
@@ -299,12 +309,19 @@ module Zeitwerk::Loader::Config
299
309
  # # ...
300
310
  # end
301
311
  #
302
- #: (String?) { (top, String) -> void } -> void ! TypeError
303
- def on_unload(cpath = :ANY, &block)
304
- raise TypeError, 'on_unload only accepts strings' unless cpath.is_a?(String) || cpath == :ANY
312
+ #: (String) { (top, String) -> void } -> void ! TypeError | NameError
313
+ #| { (String, top, String) -> void } -> void
314
+ def on_unload(cpath = UNDEFINED, &block)
315
+ key = if cpath.equal?(UNDEFINED)
316
+ :ANY
317
+ elsif !cpath.is_a?(String)
318
+ raise TypeError, 'on_unload only accepts strings'
319
+ else
320
+ @cpv.validate!(cpath)
321
+ end
305
322
 
306
323
  mutex.synchronize do
307
- (on_unload_callbacks[cpath] ||= []) << block
324
+ (on_unload_callbacks[key] ||= []) << block
308
325
  end
309
326
  end
310
327
 
@@ -0,0 +1,17 @@
1
+ # @private
2
+ class Zeitwerk::Loader::ConstantPathValidator # :nodoc
3
+ CNAME_VALIDATOR = Module.new.freeze #: Module
4
+ private_constant :CNAME_VALIDATOR
5
+
6
+ # Technically, this validation works with symbols, but API boundary restricts
7
+ # input to strings, so we assume strings, and we test strings.
8
+ #
9
+ #: (String) -> String ! NameError
10
+ def validate!(possible_cpath)
11
+ # We do this before validating because as of this writing, TruffleRuby
12
+ # raises TypeError if the argument has leading colons.
13
+ possible_cpath = possible_cpath.delete_prefix('::')
14
+ CNAME_VALIDATOR.const_defined?(possible_cpath, false)
15
+ possible_cpath
16
+ end
17
+ end
@@ -1,9 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk::Loader::Helpers
4
- CNAME_VALIDATOR = Module.new #: Module
5
- private_constant :CNAME_VALIDATOR
6
-
7
4
  #: (String, String) -> Symbol ! Zeitwerk::NameError
8
5
  private def cname_for(basename, abspath)
9
6
  cname = inflector.camelize(basename, abspath)
@@ -23,7 +20,7 @@ module Zeitwerk::Loader::Helpers
23
20
  end
24
21
 
25
22
  begin
26
- CNAME_VALIDATOR.const_defined?(cname, false)
23
+ @cpv.validate!(cname)
27
24
  rescue ::NameError => error
28
25
  path_type = @fs.rb_extension?(abspath) ? 'file' : 'directory'
29
26
 
@@ -10,6 +10,7 @@ module Zeitwerk
10
10
  require_relative 'loader/config'
11
11
  require_relative 'loader/eager_load'
12
12
  require_relative 'loader/file_system'
13
+ require_relative 'loader/constant_path_validator'
13
14
 
14
15
  extend Internal
15
16
 
@@ -115,6 +116,7 @@ module Zeitwerk
115
116
  @setup = false
116
117
  @eager_loaded = false
117
118
  @fs = FileSystem.new(self)
119
+ @cpv = ConstantPathValidator.new
118
120
 
119
121
  @mutex = Mutex.new
120
122
  @dirs_autoload_monitor = Monitor.new
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Zeitwerk
4
4
  #: String
5
- VERSION = '2.8.1'
5
+ VERSION = '2.8.2'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeitwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.1
4
+ version: 2.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
@@ -34,6 +34,7 @@ files:
34
34
  - lib/zeitwerk/loader.rb
35
35
  - lib/zeitwerk/loader/callbacks.rb
36
36
  - lib/zeitwerk/loader/config.rb
37
+ - lib/zeitwerk/loader/constant_path_validator.rb
37
38
  - lib/zeitwerk/loader/eager_load.rb
38
39
  - lib/zeitwerk/loader/file_system.rb
39
40
  - lib/zeitwerk/loader/helpers.rb