zeitwerk 2.6.6 → 2.6.8

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: 220865ab50f0336d05c8b907a08a94a8bf92843ba688c1a9686163a190b0754e
4
- data.tar.gz: a9d62662351c60a3b264a68726a84c41fc4b6f92eaae855fb4887e3eb4cf14a1
3
+ metadata.gz: 418f16f6224359c716a990f72ae916c980a3f388b09ab018c0972b8558530620
4
+ data.tar.gz: '04847c6c8f8f894fe3c2b3c39d175b5854b583d3a02ef5cca27a53cb1b8dafd4'
5
5
  SHA512:
6
- metadata.gz: b802eaabb27e6268eafa8d35d4402819551203c5fd2a1692d9a9bae65075bf668f5aecfaf3fa6c763987796b60fd5bde4704aebda4a8bfa1c74a526c27264ab5
7
- data.tar.gz: c4a53a60b49e5cb83aee148271b26fee8de15fd072327e52363438b0531d9953961305375ec727cc3a0ff2c021254eb081056350165922bd9f6f8b1e1b3e96f3
6
+ metadata.gz: f86272e84721cf9b8b1e07182b1b60cfe732ef2c266dbe076225d32af52fc9a30c278a14651ddbb3d435636646dd23c948d3fe0f00ed9b8393abdeb34dd40028
7
+ data.tar.gz: 562738b53779310e899c2065c7046844d27daabb5f354972e4c9b65bec3e5c0cdade92d19ed815fe7b0dc01538603daa3e4dd2f0049fe9a7a2e74313c0f040f6
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
 
5
5
  [![Gem Version](https://img.shields.io/gem/v/zeitwerk.svg?style=for-the-badge)](https://rubygems.org/gems/zeitwerk)
6
- [![Build Status](https://img.shields.io/github/workflow/status/fxn/zeitwerk/CI?event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions?query=event%3Apush)
6
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/fxn/zeitwerk/ci.yml?branch=main&event=push&style=for-the-badge)](https://github.com/fxn/zeitwerk/actions/workflows/ci.yml?query=branch%3main)
7
7
 
8
8
  <!-- TOC -->
9
9
 
@@ -24,6 +24,7 @@
24
24
  - [Setup](#setup)
25
25
  - [Generic](#generic)
26
26
  - [for_gem](#for_gem)
27
+ - [for_gem_extension](#for_gem_extension)
27
28
  - [Autoloading](#autoloading)
28
29
  - [Eager loading](#eager-loading)
29
30
  - [Eager load exclusions](#eager-load-exclusions)
@@ -58,9 +59,6 @@
58
59
  - [Encodings](#encodings)
59
60
  - [Rules of thumb](#rules-of-thumb)
60
61
  - [Debuggers](#debuggers)
61
- - [debug.rb](#debugrb)
62
- - [Byebug](#byebug)
63
- - [Break](#break)
64
62
  - [Pronunciation](#pronunciation)
65
63
  - [Supported Ruby versions](#supported-ruby-versions)
66
64
  - [Testing](#testing)
@@ -176,7 +174,7 @@ class HttpCrawler
176
174
  end
177
175
  ```
178
176
 
179
- The first example needs a custom [inflection](https://github.com/fxn/zeitwerk#inflection) rule:
177
+ The first example needs a custom [inflection](#inflection) rule:
180
178
 
181
179
  ```ruby
182
180
  loader.inflector.inflect("max_retries" => "MAX_RETRIES")
@@ -391,6 +389,12 @@ module MyGem
391
389
  end
392
390
  ```
393
391
 
392
+ Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`, which is the standard way to load a gem. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work.
393
+
394
+ `Zeitwerk::Loader.for_gem` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
395
+
396
+ If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](#reopening-third-party-namespaces) down below.
397
+
394
398
  Loaders returned by `Zeitwerk::Loader.for_gem` issue warnings if `lib` has extra Ruby files or directories.
395
399
 
396
400
  For example, if the gem has Rails generators under `lib/generators`, by convention that directory defines a `Generators` Ruby module. If `generators` is just a container for non-autoloadable code and templates, not acting as a project namespace, you need to setup things accordingly.
@@ -407,9 +411,64 @@ Otherwise, there's a flag to say the extra stuff is OK:
407
411
  Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
408
412
  ```
409
413
 
410
- This method is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
414
+ <a id="markdown-for_gem_extension" name="for_gem_extension"></a>
415
+ #### for_gem_extension
416
+
417
+ Let's suppose you are writing a gem to extend `Net::HTTP` with some niche feature. By [convention](https://guides.rubygems.org/name-your-gem/):
418
+
419
+ * The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
420
+ * The namespace should be `Net::HTTP::NicheFeature`.
421
+ * The entry point should be `lib/net/http/niche_feature.rb`.
422
+ * Optionally, the gem could have a top-level `lib/net-http-niche_feature.rb`, but, if defined, that one should have just a `require` call for the entry point.
423
+
424
+ The top-level file mentioned in the last point is optional. In particular, from
425
+
426
+ ```ruby
427
+ gem "net-http-niche_feature"
428
+ ```
429
+
430
+ if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
411
431
 
412
- If the entry point of your gem lives in a subdirectory of `lib` because it is reopening a namespace defined somewhere else, please use the generic API to setup the loader, and make sure you check the section [_Reopening third-party namespaces_](https://github.com/fxn/zeitwerk#reopening-third-party-namespaces) down below.
432
+ Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
433
+
434
+ The structure of the gem would be like this:
435
+
436
+ ```ruby
437
+ # lib/net-http-niche_feature.rb (optional)
438
+
439
+ # For technical reasons, this cannot be require_relative.
440
+ require "net/http/niche_feature"
441
+
442
+
443
+ # lib/net/http/niche_feature.rb
444
+
445
+ require "net/http"
446
+ require "zeitwerk"
447
+
448
+ loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
449
+ loader.setup
450
+
451
+ module Net::HTTP::NicheFeature
452
+ # Since the setup has been performed, at this point we are already able
453
+ # to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
454
+ include MyMixin
455
+ end
456
+
457
+
458
+ # lib/net/http/niche_feature/version.rb
459
+
460
+ module Net::HTTP::NicheFeature
461
+ VERSION = "1.0.0"
462
+ end
463
+ ```
464
+
465
+ `Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
466
+
467
+ If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
468
+
469
+ Due to technical reasons, the entry point of the gem has to be loaded with `Kernel#require`. Loading that file with `Kernel#load` or `Kernel#require_relative` won't generally work. This is important if you load the entry point from the optional hyphenated top-level file.
470
+
471
+ `Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
413
472
 
414
473
  <a id="markdown-autoloading" name="autoloading"></a>
415
474
  ### Autoloading
@@ -445,7 +504,7 @@ loader.eager_load
445
504
 
446
505
  That skips [ignored files and directories](#ignoring-parts-of-the-project).
447
506
 
448
- In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](https://github.com/fxn/zeitwerk#synopsis).
507
+ In gems, the method needs to be invoked after the main namespace has been defined, as shown in [Synopsis](#synopsis).
449
508
 
450
509
  Eager loading is synchronized and idempotent.
451
510
 
@@ -486,7 +545,7 @@ This is useful when the loader is not eager loading the entire project, but you
486
545
 
487
546
  Both strings and `Pathname` objects are supported as arguments. If the argument is not a directory managed by the receiver, the method raises `Zeitwerk::Error`.
488
547
 
489
- [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
548
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
490
549
 
491
550
  `Zeitwerk::Loader#eager_load_dir` is idempotent, but compatible with reloading. If you eager load a directory and then reload, eager loading that directory will load its (current) contents again.
492
551
 
@@ -519,11 +578,11 @@ root_dir3/my_app/routes
519
578
 
520
579
  where `root_directory{1,2,3}` are root directories, eager loading `MyApp::Routes` will eager load the contents of the three corresponding directories.
521
580
 
522
- There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
581
+ There might exist external source trees implementing part of the namespace. This happens routinely, because top-level constants are stored in the globally shared `Object`. It happens also when deliberately [reopening third-party namespaces](#reopening-third-party-namespaces). Such external code is not eager loaded, the implementation is carefully scoped to what the receiver manages to avoid side-effects elsewhere.
523
582
 
524
583
  This method is flexible about what it accepts. Its semantics have to be interpreted as: "_If_ you manage this namespace, or part of this namespace, please eager load what you got". In particular, if the receiver does not manage the namespace, it will simply do nothing, this is not an error condition.
525
584
 
526
- [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](https://github.com/fxn/zeitwerk#shadowed-files) are not eager loaded.
585
+ [Eager load exclusions](#eager-load-exclusions), [ignored files and directories](#ignoring-parts-of-the-project), and [shadowed files](#shadowed-files) are not eager loaded.
527
586
 
528
587
  `Zeitwerk::Loader#eager_load_namespace` is idempotent, but compatible with reloading. If you eager load a namespace and then reload, eager loading that namespace will load its (current) descendants again.
529
588
 
@@ -576,7 +635,7 @@ loader.load_file("#{__dir__}/custom_web_app/routes.rb")
576
635
 
577
636
  This is useful when the loader is not eager loading the entire project, but you still need an individual file to be loaded for things to function properly.
578
637
 
579
- Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](https://github.com/fxn/zeitwerk#shadowed-files), or is not managed by the receiver.
638
+ Both strings and `Pathname` objects are supported as arguments. The method raises `Zeitwerk::Error` if the argument is not a Ruby file, is [ignored](#ignoring-parts-of-the-project), is [shadowed](#shadowed-files), or is not managed by the receiver.
580
639
 
581
640
  `Zeitwerk::Loader#load_file` is idempotent, but compatible with reloading. If you load a file and then reload, a new call will load its (current) contents again.
582
641
 
@@ -951,6 +1010,8 @@ However, sometimes it might still be convenient to tell Zeitwerk to completely i
951
1010
 
952
1011
  You can ignore file names, directory names, and glob patterns. Glob patterns are expanded when they are added and again on each reload.
953
1012
 
1013
+ There is an edge case related to nested root directories. Conceptually, root directories are independent source trees. If you ignore a parent of a nested root directory, the nested root directory is not affected. You need to ignore it explictly if you want it ignored too.
1014
+
954
1015
  Let's see some use cases.
955
1016
 
956
1017
  <a id="markdown-use-case-files-that-do-not-follow-the-conventions" name="use-case-files-that-do-not-follow-the-conventions"></a>
@@ -1085,8 +1146,9 @@ For technical reasons, raw constant assignment is not supported:
1085
1146
 
1086
1147
  ```ruby
1087
1148
  # trip.rb
1088
- Trip = Class.new { ... } # NOT SUPPORTED
1089
- Trip = Struct.new { ... } # NOT SUPPORTED
1149
+ Trip = Class { ...} # NOT SUPPORTED
1150
+ Trip = Struct.new { ... } # NOT SUPPORTED
1151
+ Trip = Data.define { ... } # NOT SUPPORTED
1090
1152
  ```
1091
1153
 
1092
1154
  This only affects explicit namespaces, those idioms work well for any other ordinary class or module.
@@ -1169,6 +1231,8 @@ loader.push_dir(Pathname.new("/bar"), namespace: Bar)
1169
1231
  loader.dirs(namespaces: true) # => { "/foo" => Object, "/bar" => Bar }
1170
1232
  ```
1171
1233
 
1234
+ By default, ignored root directories are filtered out. If you want them included, please pass `ignored: true`.
1235
+
1172
1236
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1173
1237
 
1174
1238
  <a id="markdown-encodings" name="encodings"></a>
@@ -1201,22 +1265,9 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1201
1265
  <a id="markdown-debuggers" name="debuggers"></a>
1202
1266
  ### Debuggers
1203
1267
 
1204
- <a id="markdown-debugrb" name="debugrb"></a>
1205
- #### debug.rb
1206
-
1207
- The new [debug.rb](https://github.com/ruby/debug) gem and Zeitwerk are mostly compatible. This is the new debugger that is going to ship with Ruby 3.1.
1208
-
1209
- There's one exception, though: Due to a technical limitation of tracepoints, explicit namespaces are not autoloaded while expressions are evaluated in the REPL. See [ruby/debug#408](https://github.com/ruby/debug/issues/408).
1210
-
1211
- <a id="markdown-byebug" name="byebug"></a>
1212
- #### Byebug
1213
-
1214
- Zeitwerk and [Byebug](https://github.com/deivid-rodriguez/byebug) have a similar edge incompatibility.
1215
-
1216
- <a id="markdown-break" name="break"></a>
1217
- #### Break
1268
+ Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
1218
1269
 
1219
- Zeitwerk works fine with [@gsamokovarov](https://github.com/gsamokovarov)'s [Break](https://github.com/gsamokovarov/break) debugger.
1270
+ [Byebug](https://github.com/deivid-rodriguez/byebug) is compatible except for an edge case explained in [deivid-rodriguez/byebug#564](https://github.com/deivid-rodriguez/byebug/issues/564).
1220
1271
 
1221
1272
  <a id="markdown-pronunciation" name="pronunciation"></a>
1222
1273
  ## Pronunciation
@@ -47,6 +47,13 @@ module Zeitwerk
47
47
  disable_tracer_if_unneeded
48
48
  end
49
49
 
50
+ # This is an internal method only used by the test suite.
51
+ #
52
+ # @sig (String) -> bool
53
+ internal def registered?(cpath)
54
+ cpaths.key?(cpath)
55
+ end
56
+
50
57
  # @sig () -> void
51
58
  private def disable_tracer_if_unneeded
52
59
  mutex.synchronize do
@@ -5,8 +5,8 @@ module Zeitwerk
5
5
  # @sig (String) -> void
6
6
  def initialize(root_file)
7
7
  namespace = File.basename(root_file, ".rb")
8
- lib_dir = File.dirname(root_file)
9
- @version_file = File.join(lib_dir, namespace, "version.rb")
8
+ root_dir = File.dirname(root_file)
9
+ @version_file = File.join(root_dir, namespace, "version.rb")
10
10
  end
11
11
 
12
12
  # @sig (String, String) -> String
@@ -3,27 +3,31 @@
3
3
  module Zeitwerk
4
4
  # @private
5
5
  class GemLoader < Loader
6
+ include RealModName
7
+
6
8
  # Users should not create instances directly, the public interface is
7
9
  # `Zeitwerk::Loader.for_gem`.
8
10
  private_class_method :new
9
11
 
10
12
  # @private
11
13
  # @sig (String, bool) -> Zeitwerk::GemLoader
12
- def self._new(root_file, warn_on_extra_files:)
13
- new(root_file, warn_on_extra_files: warn_on_extra_files)
14
+ def self.__new(root_file, namespace:, warn_on_extra_files:)
15
+ new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
14
16
  end
15
17
 
16
18
  # @sig (String, bool) -> void
17
- def initialize(root_file, warn_on_extra_files:)
19
+ def initialize(root_file, namespace:, warn_on_extra_files:)
18
20
  super()
19
21
 
20
- @tag = File.basename(root_file, ".rb")
22
+ @tag = File.basename(root_file, ".rb")
23
+ @tag = real_mod_name(namespace) + "-" + @tag unless namespace.equal?(Object)
24
+
21
25
  @inflector = GemInflector.new(root_file)
22
26
  @root_file = File.expand_path(root_file)
23
- @lib = File.dirname(root_file)
27
+ @root_dir = File.dirname(root_file)
24
28
  @warn_on_extra_files = warn_on_extra_files
25
29
 
26
- push_dir(@lib)
30
+ push_dir(@root_dir, namespace: namespace)
27
31
  end
28
32
 
29
33
  # @sig () -> void
@@ -38,7 +42,7 @@ module Zeitwerk
38
42
  def warn_on_extra_files
39
43
  expected_namespace_dir = @root_file.delete_suffix(".rb")
40
44
 
41
- ls(@lib) do |basename, abspath|
45
+ ls(@root_dir) do |basename, abspath|
42
46
  next if abspath == @root_file
43
47
  next if abspath == expected_namespace_dir
44
48
 
@@ -11,18 +11,18 @@ module Zeitwerk::Loader::Callbacks
11
11
  cref = autoloads.delete(file)
12
12
  cpath = cpath(*cref)
13
13
 
14
- # If reloading is enabled, we need to put this constant for unloading
15
- # regardless of what cdef? says. In Ruby < 3.1 the internal state is not
16
- # fully cleared. Module#constants still includes it, and you need to
17
- # remove_const. See https://github.com/ruby/ruby/pull/4715.
18
- to_unload[cpath] = [file, cref] if reloading_enabled?
19
14
  Zeitwerk::Registry.unregister_autoload(file)
20
15
 
21
16
  if cdef?(*cref)
22
17
  log("constant #{cpath} loaded from file #{file}") if logger
18
+ to_unload[cpath] = [file, cref] if reloading_enabled?
23
19
  run_on_load_callbacks(cpath, cget(*cref), file) unless on_load_callbacks.empty?
24
20
  else
25
- raise Zeitwerk::NameError.new("expected file #{file} to define constant #{cpath}, but didn't", cref.last)
21
+ msg = "expected file #{file} to define constant #{cpath}, but didn't"
22
+ log(msg) if logger
23
+ crem(*cref)
24
+ to_unload[cpath] = [file, cref] if reloading_enabled?
25
+ raise Zeitwerk::NameError.new(msg, cref.last)
26
26
  end
27
27
  end
28
28
 
@@ -109,8 +109,7 @@ module Zeitwerk::Loader::Config
109
109
  # @raise [Zeitwerk::Error]
110
110
  # @sig (String | Pathname, Module) -> void
111
111
  def push_dir(path, namespace: Object)
112
- # Note that Class < Module.
113
- unless namespace.is_a?(Module)
112
+ unless namespace.is_a?(Module) # Note that Class < Module.
114
113
  raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
115
114
  end
116
115
 
@@ -149,14 +148,24 @@ module Zeitwerk::Loader::Config
149
148
  # instead. Keys are the absolute paths of the root directories as strings,
150
149
  # values are their corresponding namespaces, class or module objects.
151
150
  #
151
+ # If `ignored` is falsey (default), ignored root directories are filtered out.
152
+ #
152
153
  # These are read-only collections, please add to them with `push_dir`.
153
154
  #
154
155
  # @sig () -> Array[String] | Hash[String, Module]
155
- def dirs(namespaces: false)
156
+ def dirs(namespaces: false, ignored: false)
156
157
  if namespaces
157
- roots.clone
158
+ if ignored || ignored_paths.empty?
159
+ roots.clone
160
+ else
161
+ roots.reject { |root_dir, _namespace| ignored_path?(root_dir) }
162
+ end
158
163
  else
159
- roots.keys
164
+ if ignored || ignored_paths.empty?
165
+ roots.keys
166
+ else
167
+ roots.keys.reject { |root_dir| ignored_path?(root_dir) }
168
+ end
160
169
  end.freeze
161
170
  end
162
171
 
@@ -288,9 +297,9 @@ module Zeitwerk::Loader::Config
288
297
  # Common use case.
289
298
  return false if ignored_paths.empty?
290
299
 
291
- walk_up(abspath) do |abspath|
292
- return true if ignored_path?(abspath)
293
- return false if roots.key?(abspath)
300
+ walk_up(abspath) do |path|
301
+ return true if ignored_path?(path)
302
+ return false if roots.key?(path)
294
303
  end
295
304
 
296
305
  false
@@ -318,9 +327,9 @@ module Zeitwerk::Loader::Config
318
327
  # Optimize this common use case.
319
328
  return false if eager_load_exclusions.empty?
320
329
 
321
- walk_up(abspath) do |abspath|
322
- return true if eager_load_exclusions.member?(abspath)
323
- return false if roots.key?(abspath)
330
+ walk_up(abspath) do |path|
331
+ return true if eager_load_exclusions.member?(path)
332
+ return false if roots.key?(path)
324
333
  end
325
334
 
326
335
  false
@@ -183,7 +183,7 @@ module Zeitwerk::Loader::EagerLoad
183
183
  end
184
184
 
185
185
  # In order to invoke this method, the caller has to ensure `child` is a
186
- # strict namespace descendendant of `root_namespace`.
186
+ # strict namespace descendant of `root_namespace`.
187
187
  #
188
188
  # @sig (Module, String, Module, Boolean) -> void
189
189
  private def eager_load_child_namespace(child, child_name, root_dir, root_namespace)
@@ -134,4 +134,10 @@ module Zeitwerk::Loader::Helpers
134
134
  private def cget(parent, cname)
135
135
  parent.const_get(cname, false)
136
136
  end
137
+
138
+ # @raise [NameError]
139
+ # @sig (Module, Symbol) -> Object
140
+ private def crem(parent, cname)
141
+ parent.__send__(:remove_const, cname)
142
+ end
137
143
  end
@@ -9,6 +9,8 @@ module Zeitwerk
9
9
  require_relative "loader/config"
10
10
  require_relative "loader/eager_load"
11
11
 
12
+ extend Internal
13
+
12
14
  include RealModName
13
15
  include Callbacks
14
16
  include Helpers
@@ -26,9 +28,9 @@ module Zeitwerk
26
28
  # "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
27
29
  # ...
28
30
  #
29
- # @private
30
31
  # @sig Hash[String, [Module, Symbol]]
31
32
  attr_reader :autoloads
33
+ internal :autoloads
32
34
 
33
35
  # We keep track of autoloaded directories to remove them from the registry
34
36
  # at the end of eager loading.
@@ -36,9 +38,9 @@ module Zeitwerk
36
38
  # Files are removed as they are autoloaded, but directories need to wait due
37
39
  # to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).
38
40
  #
39
- # @private
40
41
  # @sig Array[String]
41
42
  attr_reader :autoloaded_dirs
43
+ internal :autoloaded_dirs
42
44
 
43
45
  # Stores metadata needed for unloading. Its entries look like this:
44
46
  #
@@ -52,9 +54,9 @@ module Zeitwerk
52
54
  # If reloading is enabled, this hash is filled as constants are autoloaded
53
55
  # or eager loaded. Otherwise, the collection remains empty.
54
56
  #
55
- # @private
56
57
  # @sig Hash[String, [String, [Module, Symbol]]]
57
58
  attr_reader :to_unload
59
+ internal :to_unload
58
60
 
59
61
  # Maps namespace constant paths to their respective directories.
60
62
  #
@@ -70,9 +72,9 @@ module Zeitwerk
70
72
  # and that its children are spread over those directories. We'll visit them
71
73
  # to set up the corresponding autoloads.
72
74
  #
73
- # @private
74
75
  # @sig Hash[String, Array[String]]
75
76
  attr_reader :namespace_dirs
77
+ internal :namespace_dirs
76
78
 
77
79
  # A shadowed file is a file managed by this loader that is ignored when
78
80
  # setting autoloads because its matching constant is already taken.
@@ -81,17 +83,17 @@ module Zeitwerk
81
83
  # has only scanned the top-level, `shadowed_files` does not have shadowed
82
84
  # files that may exist deep in the project tree yet.
83
85
  #
84
- # @private
85
86
  # @sig Set[String]
86
87
  attr_reader :shadowed_files
88
+ internal :shadowed_files
87
89
 
88
- # @private
89
90
  # @sig Mutex
90
91
  attr_reader :mutex
92
+ private :mutex
91
93
 
92
- # @private
93
94
  # @sig Mutex
94
95
  attr_reader :mutex2
96
+ private :mutex2
95
97
 
96
98
  def initialize
97
99
  super
@@ -134,7 +136,7 @@ module Zeitwerk
134
136
  # unload them.
135
137
  #
136
138
  # This method is public but undocumented. Main interface is `reload`, which
137
- # means `unload` + `setup`. This one is avaiable to be used together with
139
+ # means `unload` + `setup`. This one is available to be used together with
138
140
  # `unregister`, which is undocumented too.
139
141
  #
140
142
  # @sig () -> void
@@ -254,21 +256,23 @@ module Zeitwerk
254
256
  # The return value of this predicate is only meaningful if the loader has
255
257
  # scanned the file. This is the case in the spots where we use it.
256
258
  #
257
- # @private
258
259
  # @sig (String) -> Boolean
259
- def shadowed_file?(file)
260
+ internal def shadowed_file?(file)
260
261
  shadowed_files.member?(file)
261
262
  end
262
263
 
263
264
  # --- Class methods ---------------------------------------------------------------------------
264
265
 
265
266
  class << self
267
+ include RealModName
268
+
266
269
  # @sig #call | #debug | nil
267
270
  attr_accessor :default_logger
268
271
 
269
272
  # This is a shortcut for
270
273
  #
271
274
  # require "zeitwerk"
275
+ #
272
276
  # loader = Zeitwerk::Loader.new
273
277
  # loader.tag = File.basename(__FILE__, ".rb")
274
278
  # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
@@ -283,7 +287,36 @@ module Zeitwerk
283
287
  # @sig (bool) -> Zeitwerk::GemLoader
284
288
  def for_gem(warn_on_extra_files: true)
285
289
  called_from = caller_locations(1, 1).first.path
286
- Registry.loader_for_gem(called_from, warn_on_extra_files: warn_on_extra_files)
290
+ Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
291
+ end
292
+
293
+ # This is a shortcut for
294
+ #
295
+ # require "zeitwerk"
296
+ #
297
+ # loader = Zeitwerk::Loader.new
298
+ # loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb")
299
+ # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
300
+ # loader.push_dir(__dir__, namespace: namespace)
301
+ #
302
+ # except that this method returns the same object in subsequent calls from
303
+ # the same file, in the unlikely case the gem wants to be able to reload.
304
+ #
305
+ # This method returns a subclass of Zeitwerk::Loader, but the exact type
306
+ # is private, client code can only rely on the interface.
307
+ #
308
+ # @sig (bool) -> Zeitwerk::GemLoader
309
+ def for_gem_extension(namespace)
310
+ unless namespace.is_a?(Module) # Note that Class < Module.
311
+ raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
312
+ end
313
+
314
+ unless real_mod_name(namespace)
315
+ raise Zeitwerk::Error, "extending anonymous namespaces is unsupported"
316
+ end
317
+
318
+ called_from = caller_locations(1, 1).first.path
319
+ Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false)
287
320
  end
288
321
 
289
322
  # Broadcasts `eager_load` to all loaders. Those that have not been setup
@@ -323,10 +356,8 @@ module Zeitwerk
323
356
  end
324
357
  end
325
358
 
326
- private # -------------------------------------------------------------------------------------
327
-
328
359
  # @sig (String, Module) -> void
329
- def set_autoloads_in_dir(dir, parent)
360
+ private def set_autoloads_in_dir(dir, parent)
330
361
  ls(dir) do |basename, abspath|
331
362
  begin
332
363
  if ruby?(basename)
@@ -361,13 +392,22 @@ module Zeitwerk
361
392
  end
362
393
 
363
394
  # @sig (Module, Symbol, String) -> void
364
- def autoload_subdir(parent, cname, subdir)
395
+ private def autoload_subdir(parent, cname, subdir)
365
396
  if autoload_path = autoload_path_set_by_me_for?(parent, cname)
366
397
  cpath = cpath(parent, cname)
367
- register_explicit_namespace(cpath) if ruby?(autoload_path)
368
- # We do not need to issue another autoload, the existing one is enough
369
- # no matter if it is for a file or a directory. Just remember the
370
- # subdirectory has to be visited if the namespace is used.
398
+ if ruby?(autoload_path)
399
+ # Scanning visited a Ruby file first, and now a directory for the same
400
+ # constant has been found. This means we are dealing with an explicit
401
+ # namespace whose definition was seen first.
402
+ #
403
+ # Registering is idempotent, and we have to keep the autoload pointing
404
+ # to the file. This may run again if more directories are found later
405
+ # on, no big deal.
406
+ register_explicit_namespace(cpath)
407
+ end
408
+ # If the existing autoload points to a file, it has to be preserved, if
409
+ # not, it is fine as it is. In either case, we do not need to override.
410
+ # Just remember the subdirectory conforms this namespace.
371
411
  namespace_dirs[cpath] << subdir
372
412
  elsif !cdef?(parent, cname)
373
413
  # First time we find this namespace, set an autoload for it.
@@ -382,7 +422,7 @@ module Zeitwerk
382
422
  end
383
423
 
384
424
  # @sig (Module, Symbol, String) -> void
385
- def autoload_file(parent, cname, file)
425
+ private def autoload_file(parent, cname, file)
386
426
  if autoload_path = strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname))
387
427
  # First autoload for a Ruby file wins, just ignore subsequent ones.
388
428
  if ruby?(autoload_path)
@@ -408,7 +448,7 @@ module Zeitwerk
408
448
  # the file where we've found the namespace is explicitly defined.
409
449
  #
410
450
  # @sig (dir: String, file: String, parent: Module, cname: Symbol) -> void
411
- def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:)
451
+ private def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:)
412
452
  autoloads.delete(dir)
413
453
  Registry.unregister_autoload(dir)
414
454
 
@@ -419,7 +459,7 @@ module Zeitwerk
419
459
  end
420
460
 
421
461
  # @sig (Module, Symbol, String) -> void
422
- def set_autoload(parent, cname, abspath)
462
+ private def set_autoload(parent, cname, abspath)
423
463
  parent.autoload(cname, abspath)
424
464
 
425
465
  if logger
@@ -440,7 +480,7 @@ module Zeitwerk
440
480
  end
441
481
 
442
482
  # @sig (Module, Symbol) -> String?
443
- def autoload_path_set_by_me_for?(parent, cname)
483
+ private def autoload_path_set_by_me_for?(parent, cname)
444
484
  if autoload_path = strict_autoload_path(parent, cname)
445
485
  autoload_path if autoloads.key?(autoload_path)
446
486
  else
@@ -449,12 +489,12 @@ module Zeitwerk
449
489
  end
450
490
 
451
491
  # @sig (String) -> void
452
- def register_explicit_namespace(cpath)
492
+ private def register_explicit_namespace(cpath)
453
493
  ExplicitNamespace.__register(cpath, self)
454
494
  end
455
495
 
456
496
  # @sig (String) -> void
457
- def raise_if_conflicting_directory(dir)
497
+ private def raise_if_conflicting_directory(dir)
458
498
  MUTEX.synchronize do
459
499
  dir_slash = dir + "/"
460
500
 
@@ -479,23 +519,23 @@ module Zeitwerk
479
519
  end
480
520
 
481
521
  # @sig (String, Object, String) -> void
482
- def run_on_unload_callbacks(cpath, value, abspath)
522
+ private def run_on_unload_callbacks(cpath, value, abspath)
483
523
  # Order matters. If present, run the most specific one.
484
524
  on_unload_callbacks[cpath]&.each { |c| c.call(value, abspath) }
485
525
  on_unload_callbacks[:ANY]&.each { |c| c.call(cpath, value, abspath) }
486
526
  end
487
527
 
488
528
  # @sig (Module, Symbol) -> void
489
- def unload_autoload(parent, cname)
490
- parent.__send__(:remove_const, cname)
529
+ private def unload_autoload(parent, cname)
530
+ crem(parent, cname)
491
531
  log("autoload for #{cpath(parent, cname)} removed") if logger
492
532
  end
493
533
 
494
534
  # @sig (Module, Symbol) -> void
495
- def unload_cref(parent, cname)
535
+ private def unload_cref(parent, cname)
496
536
  # Let's optimistically remove_const. The way we use it, this is going to
497
537
  # succeed always if all is good.
498
- parent.__send__(:remove_const, cname)
538
+ crem(parent, cname)
499
539
  rescue ::NameError
500
540
  # There are a few edge scenarios in which this may happen. If the constant
501
541
  # is gone, that is OK, anyway.
@@ -86,8 +86,8 @@ module Zeitwerk
86
86
  #
87
87
  # @private
88
88
  # @sig (String) -> Zeitwerk::Loader
89
- def loader_for_gem(root_file, warn_on_extra_files:)
90
- gem_loaders_by_root_file[root_file] ||= GemLoader._new(root_file, warn_on_extra_files: warn_on_extra_files)
89
+ def loader_for_gem(root_file, namespace:, warn_on_extra_files:)
90
+ gem_loaders_by_root_file[root_file] ||= GemLoader.__new(root_file, namespace: namespace, warn_on_extra_files: warn_on_extra_files)
91
91
  end
92
92
 
93
93
  # @private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.6.6"
4
+ VERSION = "2.6.8"
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.6
4
+ version: 2.6.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-08 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubygems_version: 3.3.3
64
+ rubygems_version: 3.4.11
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: Efficient and thread-safe constant autoloader