zeitwerk 2.7.1 → 2.7.3
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 +4 -4
- data/lib/zeitwerk/core_ext/kernel.rb +3 -3
- data/lib/zeitwerk/core_ext/module.rb +6 -5
- data/lib/zeitwerk/cref/map.rb +159 -0
- data/lib/zeitwerk/cref.rb +17 -16
- data/lib/zeitwerk/error.rb +2 -0
- data/lib/zeitwerk/gem_inflector.rb +2 -2
- data/lib/zeitwerk/gem_loader.rb +4 -4
- data/lib/zeitwerk/inflector.rb +3 -3
- data/lib/zeitwerk/internal.rb +1 -0
- data/lib/zeitwerk/loader/callbacks.rb +19 -23
- data/lib/zeitwerk/loader/config.rb +37 -44
- data/lib/zeitwerk/loader/eager_load.rb +7 -7
- data/lib/zeitwerk/loader/helpers.rb +9 -10
- data/lib/zeitwerk/loader.rb +133 -95
- data/lib/zeitwerk/null_inflector.rb +1 -0
- data/lib/zeitwerk/real_mod_name.rb +7 -4
- data/lib/zeitwerk/registry/autoloads.rb +38 -0
- data/lib/zeitwerk/registry/explicit_namespaces.rb +61 -0
- data/lib/zeitwerk/registry/inceptions.rb +31 -0
- data/lib/zeitwerk/registry/loaders.rb +33 -0
- data/lib/zeitwerk/registry.rb +18 -96
- data/lib/zeitwerk/version.rb +2 -1
- data/lib/zeitwerk.rb +1 -2
- metadata +8 -7
- data/lib/zeitwerk/explicit_namespace.rb +0 -113
data/lib/zeitwerk/loader.rb
CHANGED
@@ -18,7 +18,7 @@ module Zeitwerk
|
|
18
18
|
include Config
|
19
19
|
include EagerLoad
|
20
20
|
|
21
|
-
MUTEX = Mutex.new
|
21
|
+
MUTEX = Mutex.new #: Mutex
|
22
22
|
private_constant :MUTEX
|
23
23
|
|
24
24
|
# Maps absolute paths for which an autoload has been set ---and not
|
@@ -28,72 +28,79 @@ module Zeitwerk
|
|
28
28
|
# "/Users/fxn/blog/app/models/hotel/pricing.rb" => #<Zeitwerk::Cref:... @mod=Hotel, @cname=:Pricing, ...>,
|
29
29
|
# ...
|
30
30
|
#
|
31
|
-
|
31
|
+
#: Hash[String, Zeitwerk::Cref]
|
32
32
|
attr_reader :autoloads
|
33
33
|
internal :autoloads
|
34
34
|
|
35
|
+
# When the path passed to Module#autoload is in the stack of features being
|
36
|
+
# loaded at the moment, Ruby passes. For example, Module#autoload? returns
|
37
|
+
# `nil` even if the autoload has not been attempted. See
|
38
|
+
#
|
39
|
+
# https://bugs.ruby-lang.org/issues/21035
|
40
|
+
#
|
41
|
+
# We call these "inceptions".
|
42
|
+
#
|
43
|
+
# A common case is the entry point of gems managed by Zeitwerk. Their main
|
44
|
+
# file is normally required and, while doing so, the loader sets an autoload
|
45
|
+
# on the gem namespace. That autoload hits this edge case.
|
46
|
+
#
|
47
|
+
# There is some logic that neeeds to know if an autoload for a given
|
48
|
+
# constant already exists. We check Module#autoload? first, and fallback to
|
49
|
+
# the inceptions just in case.
|
50
|
+
#
|
51
|
+
# This map keeps track of pairs (cref, autoload_path) found by the loader.
|
52
|
+
# The object Zeitwerk::Registry.inceptions, on the other hand, acts as a
|
53
|
+
# global registry for them.
|
54
|
+
#
|
55
|
+
#: Zeitwerk::Cref::Map[String]
|
56
|
+
attr_reader :inceptions
|
57
|
+
internal :inceptions
|
58
|
+
|
35
59
|
# We keep track of autoloaded directories to remove them from the registry
|
36
60
|
# at the end of eager loading.
|
37
61
|
#
|
38
62
|
# Files are removed as they are autoloaded, but directories need to wait due
|
39
63
|
# to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).
|
40
64
|
#
|
41
|
-
|
65
|
+
#: Array[String]
|
42
66
|
attr_reader :autoloaded_dirs
|
43
67
|
internal :autoloaded_dirs
|
44
68
|
|
45
|
-
#
|
46
|
-
#
|
47
|
-
# "Admin::Role" => [
|
48
|
-
# ".../admin/role.rb",
|
49
|
-
# #<Zeitwerk::Cref:... @mod=Admin, @cname=:Role, ...>
|
50
|
-
# ]
|
51
|
-
#
|
52
|
-
# The cpath as key helps implementing unloadable_cpath? The file name is
|
53
|
-
# stored in order to be able to delete it from $LOADED_FEATURES, and the
|
54
|
-
# cref is used to remove the constant from the parent class or module.
|
69
|
+
# If reloading is enabled, this collection maps autoload paths to their
|
70
|
+
# autoloaded crefs.
|
55
71
|
#
|
56
|
-
#
|
57
|
-
#
|
72
|
+
# On unload, the autoload paths are passed to callbacks, files deleted from
|
73
|
+
# $LOADED_FEATURES, and the crefs are deleted.
|
58
74
|
#
|
59
|
-
|
75
|
+
#: Hash[String, Zeitwerk::Cref]
|
60
76
|
attr_reader :to_unload
|
61
77
|
internal :to_unload
|
62
78
|
|
63
|
-
# Maps namespace
|
79
|
+
# Maps namespace crefs to the directories that conform the namespace.
|
64
80
|
#
|
65
|
-
#
|
81
|
+
# When these crefs get defined we know their children are spread over those
|
82
|
+
# directories. We'll visit them to set up the corresponding autoloads.
|
66
83
|
#
|
67
|
-
|
68
|
-
# "/Users/fxn/blog/app/controllers/admin",
|
69
|
-
# "/Users/fxn/blog/app/models/admin",
|
70
|
-
# ...
|
71
|
-
# ]
|
72
|
-
#
|
73
|
-
# when `Admin` gets defined we know that it plays the role of a namespace
|
74
|
-
# and that its children are spread over those directories. We'll visit them
|
75
|
-
# to set up the corresponding autoloads.
|
76
|
-
#
|
77
|
-
# @sig Hash[String, Array[String]]
|
84
|
+
#: Zeitwerk::Cref::Map[String]
|
78
85
|
attr_reader :namespace_dirs
|
79
86
|
internal :namespace_dirs
|
80
87
|
|
81
88
|
# A shadowed file is a file managed by this loader that is ignored when
|
82
89
|
# setting autoloads because its matching constant is already taken.
|
83
90
|
#
|
84
|
-
# This private set is populated as we descend. For example, if the
|
85
|
-
# has only scanned the top-level, `shadowed_files` does not have
|
86
|
-
# files that may exist deep in the project tree
|
91
|
+
# This private set is populated lazily, as we descend. For example, if the
|
92
|
+
# loader has only scanned the top-level, `shadowed_files` does not have the
|
93
|
+
# shadowed files that may exist deep in the project tree.
|
87
94
|
#
|
88
|
-
|
95
|
+
#: Set[String]
|
89
96
|
attr_reader :shadowed_files
|
90
97
|
internal :shadowed_files
|
91
98
|
|
92
|
-
|
99
|
+
#: Mutex
|
93
100
|
attr_reader :mutex
|
94
101
|
private :mutex
|
95
102
|
|
96
|
-
|
103
|
+
#: Monitor
|
97
104
|
attr_reader :dirs_autoload_monitor
|
98
105
|
private :dirs_autoload_monitor
|
99
106
|
|
@@ -101,9 +108,10 @@ module Zeitwerk
|
|
101
108
|
super
|
102
109
|
|
103
110
|
@autoloads = {}
|
111
|
+
@inceptions = Zeitwerk::Cref::Map.new
|
104
112
|
@autoloaded_dirs = []
|
105
113
|
@to_unload = {}
|
106
|
-
@namespace_dirs =
|
114
|
+
@namespace_dirs = Zeitwerk::Cref::Map.new
|
107
115
|
@shadowed_files = Set.new
|
108
116
|
@setup = false
|
109
117
|
@eager_loaded = false
|
@@ -111,12 +119,12 @@ module Zeitwerk
|
|
111
119
|
@mutex = Mutex.new
|
112
120
|
@dirs_autoload_monitor = Monitor.new
|
113
121
|
|
114
|
-
Registry.
|
122
|
+
Registry.loaders.register(self)
|
115
123
|
end
|
116
124
|
|
117
125
|
# Sets autoloads in the root namespaces.
|
118
126
|
#
|
119
|
-
|
127
|
+
#: () -> void
|
120
128
|
def setup
|
121
129
|
mutex.synchronize do
|
122
130
|
break if @setup
|
@@ -142,7 +150,7 @@ module Zeitwerk
|
|
142
150
|
# means `unload` + `setup`. This one is available to be used together with
|
143
151
|
# `unregister`, which is undocumented too.
|
144
152
|
#
|
145
|
-
|
153
|
+
#: () -> void
|
146
154
|
def unload
|
147
155
|
mutex.synchronize do
|
148
156
|
raise SetupRequired unless @setup
|
@@ -167,7 +175,7 @@ module Zeitwerk
|
|
167
175
|
end
|
168
176
|
end
|
169
177
|
|
170
|
-
to_unload.each do |
|
178
|
+
to_unload.each do |abspath, cref|
|
171
179
|
unless on_unload_callbacks.empty?
|
172
180
|
begin
|
173
181
|
value = cref.get
|
@@ -176,7 +184,7 @@ module Zeitwerk
|
|
176
184
|
# autoload failed to define the expected constant but the user
|
177
185
|
# rescued the exception.
|
178
186
|
else
|
179
|
-
run_on_unload_callbacks(
|
187
|
+
run_on_unload_callbacks(cref, value, abspath)
|
180
188
|
end
|
181
189
|
end
|
182
190
|
|
@@ -205,8 +213,10 @@ module Zeitwerk
|
|
205
213
|
namespace_dirs.clear
|
206
214
|
shadowed_files.clear
|
207
215
|
|
208
|
-
|
209
|
-
|
216
|
+
unregister_inceptions
|
217
|
+
unregister_explicit_namespaces
|
218
|
+
|
219
|
+
Registry.autoloads.unregister_loader(self)
|
210
220
|
|
211
221
|
@setup = false
|
212
222
|
@eager_loaded = false
|
@@ -219,8 +229,7 @@ module Zeitwerk
|
|
219
229
|
# This method is not thread-safe, please see how this can be achieved by
|
220
230
|
# client code in the README of the project.
|
221
231
|
#
|
222
|
-
|
223
|
-
# @sig () -> void
|
232
|
+
#: () -> void ! Zeitwerk::Error
|
224
233
|
def reload
|
225
234
|
raise ReloadingDisabledError unless reloading_enabled?
|
226
235
|
raise SetupRequired unless @setup
|
@@ -234,7 +243,7 @@ module Zeitwerk
|
|
234
243
|
# Returns a hash that maps the absolute paths of the managed files and
|
235
244
|
# directories to their respective expected constant paths.
|
236
245
|
#
|
237
|
-
|
246
|
+
#: () -> Hash[String, String]
|
238
247
|
def all_expected_cpaths
|
239
248
|
result = {}
|
240
249
|
|
@@ -264,7 +273,7 @@ module Zeitwerk
|
|
264
273
|
result
|
265
274
|
end
|
266
275
|
|
267
|
-
|
276
|
+
#: (String | Pathname) -> String?
|
268
277
|
def cpath_expected_at(path)
|
269
278
|
abspath = File.expand_path(path)
|
270
279
|
|
@@ -294,7 +303,7 @@ module Zeitwerk
|
|
294
303
|
basename = File.basename(dir)
|
295
304
|
return if hidden?(basename)
|
296
305
|
|
297
|
-
paths << [basename,
|
306
|
+
paths << [basename, dir] unless collapse?(dir)
|
298
307
|
end
|
299
308
|
|
300
309
|
return unless root_namespace
|
@@ -302,7 +311,7 @@ module Zeitwerk
|
|
302
311
|
if paths.empty?
|
303
312
|
real_mod_name(root_namespace)
|
304
313
|
else
|
305
|
-
cnames = paths.reverse_each.map {
|
314
|
+
cnames = paths.reverse_each.map { cname_for(_1, _2) }
|
306
315
|
|
307
316
|
if root_namespace == Object
|
308
317
|
cnames.join("::")
|
@@ -315,32 +324,41 @@ module Zeitwerk
|
|
315
324
|
# Says if the given constant path would be unloaded on reload. This
|
316
325
|
# predicate returns `false` if reloading is disabled.
|
317
326
|
#
|
318
|
-
#
|
327
|
+
# This is an undocumented method that I wrote to help transition from the
|
328
|
+
# classic autoloader in Rails. Its usage was removed from Rails in 7.0.
|
329
|
+
#
|
330
|
+
#: (String) -> bool
|
319
331
|
def unloadable_cpath?(cpath)
|
320
|
-
|
332
|
+
unloadable_cpaths.include?(cpath)
|
321
333
|
end
|
322
334
|
|
323
335
|
# Returns an array with the constant paths that would be unloaded on reload.
|
324
336
|
# This predicate returns an empty array if reloading is disabled.
|
325
337
|
#
|
326
|
-
#
|
338
|
+
# This is an undocumented method that I wrote to help transition from the
|
339
|
+
# classic autoloader in Rails. Its usage was removed from Rails in 7.0.
|
340
|
+
#
|
341
|
+
#: () -> Array[String]
|
327
342
|
def unloadable_cpaths
|
328
|
-
to_unload.
|
343
|
+
to_unload.values.map(&:path)
|
329
344
|
end
|
330
345
|
|
331
346
|
# This is a dangerous method.
|
332
347
|
#
|
333
348
|
# @experimental
|
334
|
-
|
349
|
+
#: () -> void
|
335
350
|
def unregister
|
351
|
+
unregister_inceptions
|
352
|
+
unregister_explicit_namespaces
|
353
|
+
Registry.loaders.unregister(self)
|
354
|
+
Registry.autoloads.unregister_loader(self)
|
336
355
|
Registry.unregister_loader(self)
|
337
|
-
ExplicitNamespace.__unregister_loader(self)
|
338
356
|
end
|
339
357
|
|
340
358
|
# The return value of this predicate is only meaningful if the loader has
|
341
359
|
# scanned the file. This is the case in the spots where we use it.
|
342
360
|
#
|
343
|
-
|
361
|
+
#: (String) -> bool
|
344
362
|
internal def shadowed_file?(file)
|
345
363
|
shadowed_files.member?(file)
|
346
364
|
end
|
@@ -350,7 +368,7 @@ module Zeitwerk
|
|
350
368
|
class << self
|
351
369
|
include RealModName
|
352
370
|
|
353
|
-
|
371
|
+
#: call(String) -> void | debug(String) -> void | nil
|
354
372
|
attr_accessor :default_logger
|
355
373
|
|
356
374
|
# This is a shortcut for
|
@@ -368,7 +386,7 @@ module Zeitwerk
|
|
368
386
|
# This method returns a subclass of Zeitwerk::Loader, but the exact type
|
369
387
|
# is private, client code can only rely on the interface.
|
370
388
|
#
|
371
|
-
|
389
|
+
#: (?warn_on_extra_files: boolish) -> Zeitwerk::GemLoader
|
372
390
|
def for_gem(warn_on_extra_files: true)
|
373
391
|
called_from = caller_locations(1, 1).first.path
|
374
392
|
Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
|
@@ -389,7 +407,7 @@ module Zeitwerk
|
|
389
407
|
# This method returns a subclass of Zeitwerk::Loader, but the exact type
|
390
408
|
# is private, client code can only rely on the interface.
|
391
409
|
#
|
392
|
-
|
410
|
+
#: (Module) -> Zeitwerk::GemLoader
|
393
411
|
def for_gem_extension(namespace)
|
394
412
|
unless namespace.is_a?(Module) # Note that Class < Module.
|
395
413
|
raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
|
@@ -406,7 +424,7 @@ module Zeitwerk
|
|
406
424
|
# Broadcasts `eager_load` to all loaders. Those that have not been setup
|
407
425
|
# are skipped.
|
408
426
|
#
|
409
|
-
|
427
|
+
#: () -> void
|
410
428
|
def eager_load_all
|
411
429
|
Registry.loaders.each do |loader|
|
412
430
|
begin
|
@@ -420,7 +438,7 @@ module Zeitwerk
|
|
420
438
|
# Broadcasts `eager_load_namespace` to all loaders. Those that have not
|
421
439
|
# been setup are skipped.
|
422
440
|
#
|
423
|
-
|
441
|
+
#: (Module) -> void
|
424
442
|
def eager_load_namespace(mod)
|
425
443
|
Registry.loaders.each do |loader|
|
426
444
|
begin
|
@@ -434,13 +452,17 @@ module Zeitwerk
|
|
434
452
|
# Returns an array with the absolute paths of the root directories of all
|
435
453
|
# registered loaders. This is a read-only collection.
|
436
454
|
#
|
437
|
-
|
455
|
+
#: () -> Array[String]
|
438
456
|
def all_dirs
|
439
|
-
|
457
|
+
dirs = []
|
458
|
+
Registry.loaders.each do |loader|
|
459
|
+
dirs.concat(loader.dirs)
|
460
|
+
end
|
461
|
+
dirs.freeze
|
440
462
|
end
|
441
463
|
end
|
442
464
|
|
443
|
-
|
465
|
+
#: (String, Module) -> void
|
444
466
|
private def define_autoloads_for_dir(dir, parent)
|
445
467
|
ls(dir) do |basename, abspath, ftype|
|
446
468
|
if ftype == :file
|
@@ -458,7 +480,7 @@ module Zeitwerk
|
|
458
480
|
end
|
459
481
|
end
|
460
482
|
|
461
|
-
|
483
|
+
#: (Zeitwerk::Cref, String) -> void
|
462
484
|
private def autoload_subdir(cref, subdir)
|
463
485
|
if autoload_path = autoload_path_set_by_me_for?(cref)
|
464
486
|
if ruby?(autoload_path)
|
@@ -474,22 +496,22 @@ module Zeitwerk
|
|
474
496
|
# If the existing autoload points to a file, it has to be preserved, if
|
475
497
|
# not, it is fine as it is. In either case, we do not need to override.
|
476
498
|
# Just remember the subdirectory conforms this namespace.
|
477
|
-
namespace_dirs[
|
499
|
+
namespace_dirs.get_or_set(cref) { [] } << subdir
|
478
500
|
elsif !cref.defined?
|
479
501
|
# First time we find this namespace, set an autoload for it.
|
480
|
-
namespace_dirs[
|
502
|
+
namespace_dirs.get_or_set(cref) { [] } << subdir
|
481
503
|
define_autoload(cref, subdir)
|
482
504
|
else
|
483
505
|
# For whatever reason the constant that corresponds to this namespace has
|
484
506
|
# already been defined, we have to recurse.
|
485
|
-
log("the namespace #{cref
|
507
|
+
log("the namespace #{cref} already exists, descending into #{subdir}") if logger
|
486
508
|
define_autoloads_for_dir(subdir, cref.get)
|
487
509
|
end
|
488
510
|
end
|
489
511
|
|
490
|
-
|
512
|
+
#: (Zeitwerk::Cref, String) -> void
|
491
513
|
private def autoload_file(cref, file)
|
492
|
-
if autoload_path = cref.autoload? || Registry.
|
514
|
+
if autoload_path = cref.autoload? || Registry.inceptions.registered?(cref)
|
493
515
|
# First autoload for a Ruby file wins, just ignore subsequent ones.
|
494
516
|
if ruby?(autoload_path)
|
495
517
|
shadowed_files << file
|
@@ -499,7 +521,7 @@ module Zeitwerk
|
|
499
521
|
end
|
500
522
|
elsif cref.defined?
|
501
523
|
shadowed_files << file
|
502
|
-
log("file #{file} is ignored because #{cref
|
524
|
+
log("file #{file} is ignored because #{cref} is already defined") if logger
|
503
525
|
else
|
504
526
|
define_autoload(cref, file)
|
505
527
|
end
|
@@ -508,12 +530,12 @@ module Zeitwerk
|
|
508
530
|
# `dir` is the directory that would have autovivified a namespace. `file` is
|
509
531
|
# the file where we've found the namespace is explicitly defined.
|
510
532
|
#
|
511
|
-
|
533
|
+
#: (dir: String, file: String, cref: Zeitwerk::Cref) -> void
|
512
534
|
private def promote_namespace_from_implicit_to_explicit(dir:, file:, cref:)
|
513
535
|
autoloads.delete(dir)
|
514
|
-
Registry.
|
536
|
+
Registry.autoloads.unregister(dir)
|
515
537
|
|
516
|
-
log("earlier autoload for #{cref
|
538
|
+
log("earlier autoload for #{cref} discarded, it is actually an explicit namespace defined in #{file}") if logger
|
517
539
|
|
518
540
|
# Order matters: When Module#const_added is triggered by the autoload, we
|
519
541
|
# don't want the namespace to be registered yet.
|
@@ -521,42 +543,58 @@ module Zeitwerk
|
|
521
543
|
register_explicit_namespace(cref)
|
522
544
|
end
|
523
545
|
|
524
|
-
|
546
|
+
#: (Zeitwerk::Cref, String) -> void
|
525
547
|
private def define_autoload(cref, abspath)
|
526
548
|
cref.autoload(abspath)
|
527
549
|
|
528
550
|
if logger
|
529
551
|
if ruby?(abspath)
|
530
|
-
log("autoload set for #{cref
|
552
|
+
log("autoload set for #{cref}, to be loaded from #{abspath}")
|
531
553
|
else
|
532
|
-
log("autoload set for #{cref
|
554
|
+
log("autoload set for #{cref}, to be autovivified from #{abspath}")
|
533
555
|
end
|
534
556
|
end
|
535
557
|
|
536
558
|
autoloads[abspath] = cref
|
537
|
-
Registry.
|
559
|
+
Registry.autoloads.register(abspath, self)
|
538
560
|
|
539
|
-
|
540
|
-
unless cref.autoload?
|
541
|
-
Registry.register_inception(cref.path, abspath, self)
|
542
|
-
end
|
561
|
+
register_inception(cref, abspath) unless cref.autoload?
|
543
562
|
end
|
544
563
|
|
545
|
-
|
564
|
+
#: (Zeitwerk::Cref) -> String?
|
546
565
|
private def autoload_path_set_by_me_for?(cref)
|
547
566
|
if autoload_path = cref.autoload?
|
548
567
|
autoload_path if autoloads.key?(autoload_path)
|
549
568
|
else
|
550
|
-
|
569
|
+
inceptions[cref]
|
551
570
|
end
|
552
571
|
end
|
553
572
|
|
554
|
-
|
573
|
+
#: (Zeitwerk::Cref) -> void
|
555
574
|
private def register_explicit_namespace(cref)
|
556
|
-
|
575
|
+
Registry.explicit_namespaces.register(cref, self)
|
576
|
+
end
|
577
|
+
|
578
|
+
#: () -> void
|
579
|
+
private def unregister_explicit_namespaces
|
580
|
+
Registry.explicit_namespaces.unregister_loader(self)
|
581
|
+
end
|
582
|
+
|
583
|
+
#: (Zeitwerk::Cref, String) -> void
|
584
|
+
private def register_inception(cref, abspath)
|
585
|
+
inceptions[cref] = abspath
|
586
|
+
Registry.inceptions.register(cref, abspath)
|
587
|
+
end
|
588
|
+
|
589
|
+
#: () -> void
|
590
|
+
private def unregister_inceptions
|
591
|
+
inceptions.each_key do |cref|
|
592
|
+
Registry.inceptions.unregister(cref)
|
593
|
+
end
|
594
|
+
inceptions.clear
|
557
595
|
end
|
558
596
|
|
559
|
-
|
597
|
+
#: (String) -> void
|
560
598
|
private def raise_if_conflicting_directory(dir)
|
561
599
|
MUTEX.synchronize do
|
562
600
|
dir_slash = dir + "/"
|
@@ -580,20 +618,20 @@ module Zeitwerk
|
|
580
618
|
end
|
581
619
|
end
|
582
620
|
|
583
|
-
|
584
|
-
private def run_on_unload_callbacks(
|
621
|
+
#: (String, top, String) -> void
|
622
|
+
private def run_on_unload_callbacks(cref, value, abspath)
|
585
623
|
# Order matters. If present, run the most specific one.
|
586
|
-
on_unload_callbacks[
|
587
|
-
on_unload_callbacks[:ANY]&.each { |c| c.call(
|
624
|
+
on_unload_callbacks[cref.path]&.each { |c| c.call(value, abspath) }
|
625
|
+
on_unload_callbacks[:ANY]&.each { |c| c.call(cref.path, value, abspath) }
|
588
626
|
end
|
589
627
|
|
590
|
-
|
628
|
+
#: (Zeitwerk::Cref) -> void
|
591
629
|
private def unload_autoload(cref)
|
592
630
|
cref.remove
|
593
|
-
log("autoload for #{cref
|
631
|
+
log("autoload for #{cref} removed") if logger
|
594
632
|
end
|
595
633
|
|
596
|
-
|
634
|
+
#: (Zeitwerk::Cref) -> void
|
597
635
|
private def unload_cref(cref)
|
598
636
|
# Let's optimistically remove_const. The way we use it, this is going to
|
599
637
|
# succeed always if all is good.
|
@@ -602,7 +640,7 @@ module Zeitwerk
|
|
602
640
|
# There are a few edge scenarios in which this may happen. If the constant
|
603
641
|
# is gone, that is OK, anyway.
|
604
642
|
else
|
605
|
-
log("#{cref
|
643
|
+
log("#{cref} unloaded") if logger
|
606
644
|
end
|
607
645
|
end
|
608
646
|
end
|
@@ -1,15 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Zeitwerk::RealModName
|
4
|
+
#: UnboundMethod
|
4
5
|
UNBOUND_METHOD_MODULE_NAME = Module.instance_method(:name)
|
5
6
|
private_constant :UNBOUND_METHOD_MODULE_NAME
|
6
7
|
|
7
|
-
# Returns the real name of the class or module
|
8
|
-
# constant to which it was assigned (or nil).
|
8
|
+
# Returns the real name of the class or module.
|
9
9
|
#
|
10
|
-
#
|
10
|
+
# We need this indirection becasue the `name` method can be overridden, and
|
11
|
+
# because in practice what we really need is the constant paths of modules
|
12
|
+
# with a permanent name, not so much what the user considers to be the name of
|
13
|
+
# a certain class or module of theirs.
|
11
14
|
#
|
12
|
-
|
15
|
+
#: (Module) -> String?
|
13
16
|
def real_mod_name(mod)
|
14
17
|
UNBOUND_METHOD_MODULE_NAME.bind_call(mod)
|
15
18
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Zeitwerk::Registry
|
2
|
+
class Autoloads # :nodoc:
|
3
|
+
#: () -> void
|
4
|
+
def initialize
|
5
|
+
@autoloads = {} #: Hash[String, Zeitwerk::Loader]
|
6
|
+
end
|
7
|
+
|
8
|
+
#: (String, Zeitwerk::Loader) -> Zeitwerk::Loader
|
9
|
+
def register(abspath, loader)
|
10
|
+
@autoloads[abspath] = loader
|
11
|
+
end
|
12
|
+
|
13
|
+
#: (String) -> Zeitwerk::Loader?
|
14
|
+
def registered?(path)
|
15
|
+
@autoloads[path]
|
16
|
+
end
|
17
|
+
|
18
|
+
#: (String) -> Zeitwerk::Loader?
|
19
|
+
def unregister(abspath)
|
20
|
+
@autoloads.delete(abspath)
|
21
|
+
end
|
22
|
+
|
23
|
+
#: (Zeitwerk::Loader) -> void
|
24
|
+
def unregister_loader(loader)
|
25
|
+
@autoloads.delete_if { _2 == loader }
|
26
|
+
end
|
27
|
+
|
28
|
+
#: () -> bool
|
29
|
+
def empty? # for tests
|
30
|
+
@autoloads.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
#: () -> void
|
34
|
+
def clear # for tests
|
35
|
+
@autoloads.clear
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zeitwerk::Registry
|
4
|
+
# A registry for explicit namespaces.
|
5
|
+
#
|
6
|
+
# When a loader determines that a certain file should define an explicit
|
7
|
+
# namespace, it registers it here, associating its cref with itself.
|
8
|
+
#
|
9
|
+
# If the namespace is autoloaded, our const_added callback retrieves its
|
10
|
+
# loader by calling loader_for. That way, the loader is able to scan the
|
11
|
+
# subdirectories that conform the namespace and set autoloads for their
|
12
|
+
# expected constants just in time.
|
13
|
+
#
|
14
|
+
# Once autoloaded, the namespace is unregistered.
|
15
|
+
#
|
16
|
+
# The implementation assumes an explicit namespace is managed by one loader.
|
17
|
+
# Loaders that reopen namespaces owned by other projects are responsible for
|
18
|
+
# loading their constant before setup. This is documented.
|
19
|
+
#
|
20
|
+
# **This is a private module.**
|
21
|
+
class ExplicitNamespaces # :nodoc: all
|
22
|
+
#: () -> void
|
23
|
+
def initialize
|
24
|
+
# Maps crefs of explicit namespaces with their corresponding loader.
|
25
|
+
#
|
26
|
+
# Entries are added as the namespaces are found, and removed as they are
|
27
|
+
# autoloaded.
|
28
|
+
@loaders = Zeitwerk::Cref::Map.new
|
29
|
+
end
|
30
|
+
|
31
|
+
# Registers `cref` as being the constant path of an explicit namespace
|
32
|
+
# managed by `loader`.
|
33
|
+
#
|
34
|
+
#: (Zeitwerk::Cref, Zeitwerk::Loader) -> void
|
35
|
+
def register(cref, loader)
|
36
|
+
@loaders[cref] = loader
|
37
|
+
end
|
38
|
+
|
39
|
+
#: (Module, Symbol) -> Zeitwerk::Loader?
|
40
|
+
def loader_for(mod, cname)
|
41
|
+
@loaders.delete_mod_cname(mod, cname)
|
42
|
+
end
|
43
|
+
|
44
|
+
#: (Zeitwerk::Loader) -> void
|
45
|
+
def unregister_loader(loader)
|
46
|
+
@loaders.delete_by_value(loader)
|
47
|
+
end
|
48
|
+
|
49
|
+
# This is an internal method only used by the test suite.
|
50
|
+
#
|
51
|
+
#: (Zeitwerk::Cref) -> Zeitwerk::Loader?
|
52
|
+
def registered?(cref)
|
53
|
+
@loaders[cref]
|
54
|
+
end
|
55
|
+
|
56
|
+
#: () -> void
|
57
|
+
def clear # for tests
|
58
|
+
@loaders.clear
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Zeitwerk::Registry
|
2
|
+
# Loaders know their own inceptions, but there is a use case in which we need
|
3
|
+
# to know if a given cpath is an inception globally. This is what this
|
4
|
+
# registry is for.
|
5
|
+
class Inceptions # :nodoc:
|
6
|
+
#: () -> void
|
7
|
+
def initialize
|
8
|
+
@inceptions = Zeitwerk::Cref::Map.new #: Zeitwerk::Cref::Map[String]
|
9
|
+
end
|
10
|
+
|
11
|
+
#: (Zeitwerk::Cref, String) -> void
|
12
|
+
def register(cref, abspath)
|
13
|
+
@inceptions[cref] = abspath
|
14
|
+
end
|
15
|
+
|
16
|
+
#: (Zeitwerk::Cref) -> String?
|
17
|
+
def registered?(cref)
|
18
|
+
@inceptions[cref]
|
19
|
+
end
|
20
|
+
|
21
|
+
#: (Zeitwerk::Cref) -> void
|
22
|
+
def unregister(cref)
|
23
|
+
@inceptions.delete(cref)
|
24
|
+
end
|
25
|
+
|
26
|
+
#: () -> void
|
27
|
+
def clear # for tests
|
28
|
+
@inceptions.clear
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|