zeitwerk 2.6.7 → 2.6.9

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: e6c7bf12c3a33195c57541b840ca6d7dbc21b19e0d7b91d8933ad37aa88b325d
4
- data.tar.gz: 4b8dd86417dd633b78c02eada9ed6956e8e708397c988237aaddecb9644ba469
3
+ metadata.gz: b1de024fb1ddb2f4bc64ced5aacf898f01b6336d5d8b85701d0757e256d062fb
4
+ data.tar.gz: d3f06cb22aad419d5c1778fc4a13e1cc036cd062b5ac1fcece5eb4fa3ba1ce00
5
5
  SHA512:
6
- metadata.gz: 4f0bc60d9914a9e815aed501a030f7e9bde7927f750f3836f96ac86a52c0699f4ca69456bd381845428f6b09adccaa085443f39bb0fdaa651437742e21d10af2
7
- data.tar.gz: 3ba55f7bb24725d156cc4043697bfcd6092e2893f1aa71296965c82ae9ed66b79c30f853b535693b6b884d7d3d26cf1b122545e395ba11c92f65552e14116248
6
+ metadata.gz: 3b49724afa7c8097eb6014264faa50f6320c7686760fc5677f6c648f6f4c482b2aa6efb8f78621721c6b00ed424a0a77864cdf8fafb2a481a0ddd1c333115d04
7
+ data.tar.gz: 01b7782fea2affe16c5241f9325649e90f3beb3150f168cecf6d710fdfffb66a9bfa853ca4ce562ddbdca3133718b7b2bdb2b1260cf997724d668d6639a5199b
data/README.md CHANGED
@@ -3,7 +3,8 @@
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/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)
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%3Amain)
7
+
7
8
 
8
9
  <!-- TOC -->
9
10
 
@@ -24,6 +25,7 @@
24
25
  - [Setup](#setup)
25
26
  - [Generic](#generic)
26
27
  - [for_gem](#for_gem)
28
+ - [for_gem_extension](#for_gem_extension)
27
29
  - [Autoloading](#autoloading)
28
30
  - [Eager loading](#eager-loading)
29
31
  - [Eager load exclusions](#eager-load-exclusions)
@@ -55,6 +57,8 @@
55
57
  - [Beware of circular dependencies](#beware-of-circular-dependencies)
56
58
  - [Reopening third-party namespaces](#reopening-third-party-namespaces)
57
59
  - [Introspection](#introspection)
60
+ - [`Zeitwerk::Loader#dirs`](#zeitwerkloaderdirs)
61
+ - [`Zeitwerk::Loader#cpath_expected_at`](#zeitwerkloadercpath_expected_at)
58
62
  - [Encodings](#encodings)
59
63
  - [Rules of thumb](#rules-of-thumb)
60
64
  - [Debuggers](#debuggers)
@@ -75,15 +79,15 @@
75
79
 
76
80
  Zeitwerk is an efficient and thread-safe code loader for Ruby.
77
81
 
78
- Given a [conventional file structure](#file-structure), Zeitwerk is able to load your project's classes and modules on demand (autoloading), or upfront (eager loading). You don't need to write `require` calls for your own files, rather, you can streamline your programming knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and matches Ruby's semantics for constants.
82
+ Given a [conventional file structure](#file-structure), Zeitwerk is capable of loading your project's classes and modules on demand (autoloading) or upfront (eager loading). You don't need to write `require` calls for your own files; instead, you can streamline your programming by knowing that your classes and modules are available everywhere. This feature is efficient, thread-safe, and aligns with Ruby's semantics for constants.
79
83
 
80
- Zeitwerk is also able to reload code, which may be handy while developing web applications. Coordination is needed to reload in a thread-safe manner. The documentation below explains how to do this.
84
+ Zeitwerk also supports code reloading, which can be useful during web application development. However, coordination is required to reload in a thread-safe manner. The documentation below explains how to achieve this.
81
85
 
82
- The gem is designed so that any project, gem dependency, application, etc. can have their own independent loader, coexisting in the same process, managing their own project trees, and independent of each other. Each loader has its own configuration, inflector, and optional logger.
86
+ The gem is designed to allow any project, gem dependency, or application to have its own independent loader. Multiple loaders can coexist in the same process, each managing its own project tree and operating independently of each other. Each loader has its own configuration, inflector, and optional logger.
83
87
 
84
- Internally, Zeitwerk issues `require` calls exclusively using absolute file names, so there are no costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk do not even need to be in `$LOAD_PATH`.
88
+ Internally, Zeitwerk exclusively uses absolute file names when issuing `require` calls, eliminating the need for costly file system lookups in `$LOAD_PATH`. Technically, the directories managed by Zeitwerk don't even need to be in `$LOAD_PATH`.
85
89
 
86
- Furthermore, Zeitwerk does at most one single scan of the project tree, and it descends into subdirectories lazily, only if their namespaces are used.
90
+ Furthermore, Zeitwerk performs a single scan of the project tree at most, lazily descending into subdirectories only when their namespaces are used.
87
91
 
88
92
  <a id="markdown-synopsis" name="synopsis"></a>
89
93
  ## Synopsis
@@ -143,7 +147,7 @@ Zeitwerk::Loader.eager_load_all
143
147
  <a id="markdown-the-idea-file-paths-match-constant-paths" name="the-idea-file-paths-match-constant-paths"></a>
144
148
  ### The idea: File paths match constant paths
145
149
 
146
- To have a file structure Zeitwerk can work with, just name files and directories after the name of the classes and modules they define:
150
+ For Zeitwerk to work with your file structure, simply name files and directories after the classes and modules they define:
147
151
 
148
152
  ```
149
153
  lib/my_gem.rb -> MyGem
@@ -152,7 +156,7 @@ lib/my_gem/bar_baz.rb -> MyGem::BarBaz
152
156
  lib/my_gem/woo/zoo.rb -> MyGem::Woo::Zoo
153
157
  ```
154
158
 
155
- You can tune that a bit by [collapsing directories](#collapsing-directories), or by [ignoring parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
159
+ You can fine-tune this behavior by [collapsing directories](#collapsing-directories) or [ignoring specific parts of the project](#ignoring-parts-of-the-project), but that is the main idea.
156
160
 
157
161
  <a id="markdown-inner-simple-constants" name="inner-simple-constants"></a>
158
162
  ### Inner simple constants
@@ -210,7 +214,7 @@ serializers/user_serializer.rb -> UserSerializer
210
214
  <a id="markdown-custom-root-namespaces" name="custom-root-namespaces"></a>
211
215
  #### Custom root namespaces
212
216
 
213
- While `Object` is by far the most common root namespace, you can associate a different one to a particular root directory. The method `push_dir` accepts a non-anonymous class or module object in the optional `namespace` keyword argument.
217
+ Although `Object` is the most common root namespace, you have the flexibility to associate a different one with a specific root directory. The `push_dir` method accepts a non-anonymous class or module object as the optional `namespace` keyword argument.
214
218
 
215
219
  For example, given:
216
220
 
@@ -226,14 +230,14 @@ a file defining `ActiveJob::QueueAdapters::MyQueueAdapter` does not need the con
226
230
  adapters/my_queue_adapter.rb -> ActiveJob::QueueAdapters::MyQueueAdapter
227
231
  ```
228
232
 
229
- Please, note that the given root namespace must be non-reloadable, though autoloaded constants in that namespace can be. That is, if you associate `app/api` with an existing `Api` module, that module should not be reloadable. However, if the project defines and autoloads the class `Api::Deliveries`, that one can be reloaded.
233
+ Please note that the provided root namespace must be non-reloadable, while allowing autoloaded constants within that namespace to be reloadable. This means that if you associate the `app/api` directory with an existing `Api` module, the module itself should not be reloadable. However, if the project defines and autoloads the `Api::Deliveries` class, that class can be reloaded.
230
234
 
231
235
  <a id="markdown-nested-root-directories" name="nested-root-directories"></a>
232
236
  #### Nested root directories
233
237
 
234
- Root directories should not be ideally nested, but Zeitwerk supports them because in Rails, for example, both `app/models` and `app/models/concerns` belong to the autoload paths.
238
+ Root directories are recommended not to be nested; however, Zeitwerk provides support for nested root directories since in frameworks like Rails, both `app/models` and `app/models/concerns` belong to the autoload paths.
235
239
 
236
- Zeitwerk detects nested root directories, and treats them as roots only. In the example above, `concerns` is not considered to be a namespace below `app/models`. For example, the file:
240
+ Zeitwerk identifies nested root directories and treats them as independent roots. In the given example, `concerns` is not considered a namespace within `app/models`. For instance, consider the following file:
237
241
 
238
242
  ```
239
243
  app/models/concerns/geolocatable.rb
@@ -244,9 +248,9 @@ should define `Geolocatable`, not `Concerns::Geolocatable`.
244
248
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
245
249
  ### Implicit namespaces
246
250
 
247
- If a namespace is just a simple module with no code, you do not need to define it in a file: Directories without a matching Ruby file get modules created automatically on your behalf.
251
+ If a namespace consists only of a simple module without any code, there is no need to explicitly define it in a separate file. Zeitwerk automatically creates modules on your behalf for directories without a corresponding Ruby file.
248
252
 
249
- For example, if a project has an `admin` directory:
253
+ For instance, suppose a project includes an `admin` directory:
250
254
 
251
255
  ```
252
256
  app/controllers/admin/users_controller.rb -> Admin::UsersController
@@ -254,7 +258,7 @@ app/controllers/admin/users_controller.rb -> Admin::UsersController
254
258
 
255
259
  and does not have a file called `admin.rb`, Zeitwerk automatically creates an `Admin` module on your behalf the first time `Admin` is used.
256
260
 
257
- For this to happen, the directory has to contain non-ignored Ruby files with extension `.rb`, directly or recursively, otherwise it is ignored. This condition is evaluated again on reloads.
261
+ To trigger this behavior, the directory must contain non-ignored Ruby files with the `.rb` extension, either directly or recursively. Otherwise, the directory is ignored. This condition is reevaluated during reloads.
258
262
 
259
263
  <a id="markdown-explicit-namespaces" name="explicit-namespaces"></a>
260
264
  ### Explicit namespaces
@@ -410,6 +414,65 @@ Otherwise, there's a flag to say the extra stuff is OK:
410
414
  Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
411
415
  ```
412
416
 
417
+ <a id="markdown-for_gem_extension" name="for_gem_extension"></a>
418
+ #### for_gem_extension
419
+
420
+ 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/):
421
+
422
+ * The gem should be called `net-http-niche_feature`. That is, hyphens for the extended part, a hyphen, and underscores for yours.
423
+ * The namespace should be `Net::HTTP::NicheFeature`.
424
+ * The entry point should be `lib/net/http/niche_feature.rb`.
425
+ * 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.
426
+
427
+ The top-level file mentioned in the last point is optional. In particular, from
428
+
429
+ ```ruby
430
+ gem "net-http-niche_feature"
431
+ ```
432
+
433
+ if the hyphenated file does not exist, Bundler notes the conventional hyphenated pattern and issues a `require` for `net/http/niche_feature`.
434
+
435
+ Gem extensions following the conventions above have a dedicated loader constructor: `Zeitwerk::Loader.for_gem_extension`.
436
+
437
+ The structure of the gem would be like this:
438
+
439
+ ```ruby
440
+ # lib/net-http-niche_feature.rb (optional)
441
+
442
+ # For technical reasons, this cannot be require_relative.
443
+ require "net/http/niche_feature"
444
+
445
+
446
+ # lib/net/http/niche_feature.rb
447
+
448
+ require "net/http"
449
+ require "zeitwerk"
450
+
451
+ loader = Zeitwerk::Loader.for_gem_extension(Net::HTTP)
452
+ loader.setup
453
+
454
+ module Net::HTTP::NicheFeature
455
+ # Since the setup has been performed, at this point we are already able
456
+ # to reference project constants, in this case Net::HTTP::NicheFeature::MyMixin.
457
+ include MyMixin
458
+ end
459
+
460
+
461
+ # lib/net/http/niche_feature/version.rb
462
+
463
+ module Net::HTTP::NicheFeature
464
+ VERSION = "1.0.0"
465
+ end
466
+ ```
467
+
468
+ `Zeitwerk::Loader.for_gem_extension` expects as argument the namespace being extended, which has to be a non-anonymous class or module object.
469
+
470
+ If it exists, `lib/net/http/niche_feature/version.rb` is expected to define `Net::HTTP::NicheFeature::VERSION`.
471
+
472
+ 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.
473
+
474
+ `Zeitwerk::Loader.for_gem_extension` is idempotent when invoked from the same file, to support gems that want to reload (unlikely).
475
+
413
476
  <a id="markdown-autoloading" name="autoloading"></a>
414
477
  ### Autoloading
415
478
 
@@ -673,9 +736,34 @@ loader.inflector.inflect "html_parser" => "HTMLParser"
673
736
  loader.inflector.inflect "mysql_adapter" => "MySQLAdapter"
674
737
  ```
675
738
 
739
+ Overrides have to match exactly directory or file (without extension) _basenames_. For example, if you configure
740
+
741
+ ```ruby
742
+ loader.inflector.inflect("xml" => "XML")
743
+ ```
744
+
745
+ then the following constants are expected:
746
+
747
+ ```
748
+ xml.rb -> XML
749
+ foo/xml -> Foo::XML
750
+ foo/bar/xml.rb -> Foo::Bar::XML
751
+ ```
752
+
753
+ As you see, any directory whose basename is exactly `xml`, and any file whose basename is exactly `xml.rb` are expected to define the constant `XML` in the corresponding namespace. On the other hand, partial matches are ignored. For example, `xml_parser.rb` would be inflected as `XmlParser` because `xml_parser` is not equal to `xml`. You'd need an additional override:
754
+
755
+ ```ruby
756
+ loader.inflector.inflect(
757
+ "xml" => "XML",
758
+ "xml_parser" => "XMLParser"
759
+ )
760
+ ```
761
+
762
+ If you need more flexibility, you can define a custom inflector, as explained down below.
763
+
676
764
  Overrides need to be configured before calling `setup`.
677
765
 
678
- The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
766
+ The inflectors of different loaders are independent of each other. There are no global inflection rules or global configuration that can affect this inflector. It is deterministic.
679
767
 
680
768
  <a id="markdown-zeitwerkgeminflector" name="zeitwerkgeminflector"></a>
681
769
  #### Zeitwerk::GemInflector
@@ -1154,6 +1242,9 @@ With that, when Zeitwerk scans the file system and reaches the gem directories `
1154
1242
  <a id="markdown-introspection" name="introspection"></a>
1155
1243
  ### Introspection
1156
1244
 
1245
+ <a id="markdown-zeitwerkloaderdirs" name="zeitwerkloaderdirs"></a>
1246
+ #### `Zeitwerk::Loader#dirs`
1247
+
1157
1248
  The method `Zeitwerk::Loader#dirs` returns an array with the absolute paths of the root directories as strings:
1158
1249
 
1159
1250
  ```ruby
@@ -1175,6 +1266,44 @@ By default, ignored root directories are filtered out. If you want them included
1175
1266
 
1176
1267
  These collections are read-only. Please add to them with `Zeitwerk::Loader#push_dir`.
1177
1268
 
1269
+ <a id="markdown-zeitwerkloadercpath_expected_at" name="zeitwerkloadercpath_expected_at"></a>
1270
+ #### `Zeitwerk::Loader#cpath_expected_at`
1271
+
1272
+ Given a path as a string or `Pathname` object, `Zeitwerk::Loader#cpath_expected_at` returns a string with the corresponding expected constant path.
1273
+
1274
+ Some examples, assuming that `app/models` is a root directory:
1275
+
1276
+ ```ruby
1277
+ loader.cpath_expected_at("app/models") # => "Object"
1278
+ loader.cpath_expected_at("app/models/user.rb") # => "User"
1279
+ loader.cpath_expected_at("app/models/hotel") # => "Hotel"
1280
+ loader.cpath_expected_at("app/models/hotel/billing.rb") # => "Hotel::Billing"
1281
+ ```
1282
+
1283
+ If `collapsed` is a collapsed directory:
1284
+
1285
+ ```ruby
1286
+ loader.cpath_expected_at("a/b/collapsed/c") # => "A::B::C"
1287
+ loader.cpath_expected_at("a/b/collapsed") # => "A::B", edge case
1288
+ loader.cpath_expected_at("a/b") # => "A::B"
1289
+ ```
1290
+
1291
+ If the argument corresponds to an [ignored file or directory](#ignoring-parts-of-the-project), the method returns `nil`. Same if the argument is not managed by the loader.
1292
+
1293
+ `Zeitwerk::Error` is raised if the given path does not exist:
1294
+
1295
+ ```ruby
1296
+ loader.cpath_expected_at("non_existing_file.rb") # => Zeitwerk::Error
1297
+ ```
1298
+
1299
+ `Zeitwer::NameError` is raised if a constant path cannot be derived from it:
1300
+
1301
+ ```ruby
1302
+ loader.cpath_expected_at("8.rb") # => Zeitwerk::NameError
1303
+ ```
1304
+
1305
+ This method does not parse file contents and does not guarantee files define the returned constant path. It just says which is the _expected_ one.
1306
+
1178
1307
  <a id="markdown-encodings" name="encodings"></a>
1179
1308
  ### Encodings
1180
1309
 
@@ -1205,9 +1334,11 @@ The test suite passes on Windows with codepage `Windows-1252` if all the involve
1205
1334
  <a id="markdown-debuggers" name="debuggers"></a>
1206
1335
  ### Debuggers
1207
1336
 
1208
- Zeitwerk works fine with [debug.rb](https://github.com/ruby/debug) and [Break](https://github.com/gsamokovarov/break).
1337
+ Zeitwerk and [debug.rb](https://github.com/ruby/debug) are fully compatible if CRuby is ≥ 3.1 (see [ruby/debug#558](https://github.com/ruby/debug/pull/558)).
1338
+
1339
+ [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). Prior to CRuby 3.1, `debug.rb` has a similar edge incompatibility.
1209
1340
 
1210
- [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).
1341
+ [Break](https://github.com/gsamokovarov/break) is fully compatible.
1211
1342
 
1212
1343
  <a id="markdown-pronunciation" name="pronunciation"></a>
1213
1344
  ## Pronunciation
@@ -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
 
@@ -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
 
@@ -298,9 +297,9 @@ module Zeitwerk::Loader::Config
298
297
  # Common use case.
299
298
  return false if ignored_paths.empty?
300
299
 
301
- walk_up(abspath) do |abspath|
302
- return true if ignored_path?(abspath)
303
- 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)
304
303
  end
305
304
 
306
305
  false
@@ -328,9 +327,9 @@ module Zeitwerk::Loader::Config
328
327
  # Optimize this common use case.
329
328
  return false if eager_load_exclusions.empty?
330
329
 
331
- walk_up(abspath) do |abspath|
332
- return true if eager_load_exclusions.member?(abspath)
333
- 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)
334
333
  end
335
334
 
336
335
  false
@@ -45,8 +45,10 @@ module Zeitwerk::Loader::EagerLoad
45
45
 
46
46
  break if root_namespace = roots[dir]
47
47
 
48
+ basename = File.basename(dir)
49
+ return if hidden?(basename)
50
+
48
51
  unless collapse?(dir)
49
- basename = File.basename(dir)
50
52
  cnames << inflector.camelize(basename, dir).to_sym
51
53
  end
52
54
  end
@@ -119,6 +121,8 @@ module Zeitwerk::Loader::EagerLoad
119
121
  raise Zeitwerk::Error.new("#{abspath} is ignored") if ignored_path?(abspath)
120
122
 
121
123
  basename = File.basename(abspath, ".rb")
124
+ raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
125
+
122
126
  base_cname = inflector.camelize(basename, abspath).to_sym
123
127
 
124
128
  root_namespace = nil
@@ -129,8 +133,10 @@ module Zeitwerk::Loader::EagerLoad
129
133
 
130
134
  break if root_namespace = roots[dir]
131
135
 
136
+ basename = File.basename(dir)
137
+ raise Zeitwerk::Error.new("#{abspath} is ignored") if hidden?(basename)
138
+
132
139
  unless collapse?(dir)
133
- basename = File.basename(dir)
134
140
  cnames << inflector.camelize(basename, dir).to_sym
135
141
  end
136
142
  end
@@ -228,6 +228,69 @@ module Zeitwerk
228
228
  setup
229
229
  end
230
230
 
231
+ # @sig (String | Pathname) -> String?
232
+ def cpath_expected_at(path)
233
+ abspath = File.expand_path(path)
234
+
235
+ raise Zeitwerk::Error.new("#{abspath} does not exist") unless File.exist?(abspath)
236
+
237
+ return unless dir?(abspath) || ruby?(abspath)
238
+ return if ignored_path?(abspath)
239
+
240
+ cnames = []
241
+ abspaths = []
242
+
243
+ if ruby?(abspath)
244
+ basename = File.basename(abspath, ".rb")
245
+ return if hidden?(basename)
246
+
247
+ cnames << inflector.camelize(basename, abspath).to_sym
248
+ abspaths << abspath
249
+ walk_up_from = File.dirname(abspath)
250
+ else
251
+ walk_up_from = abspath
252
+ end
253
+
254
+ root_namespace = nil
255
+
256
+ walk_up(walk_up_from) do |dir|
257
+ break if root_namespace = roots[dir]
258
+ return if ignored_path?(dir)
259
+
260
+ basename = File.basename(dir)
261
+ return if hidden?(basename)
262
+
263
+ unless collapse?(dir)
264
+ cnames << inflector.camelize(basename, dir).to_sym
265
+ abspaths << dir
266
+ end
267
+ end
268
+
269
+ return unless root_namespace
270
+
271
+ if cnames.empty?
272
+ real_mod_name(root_namespace)
273
+ 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
285
+
286
+ if root_namespace == Object
287
+ cnames.join("::")
288
+ else
289
+ "#{real_mod_name(root_namespace)}::#{cnames.join("::")}"
290
+ end
291
+ end
292
+ end
293
+
231
294
  # Says if the given constant path would be unloaded on reload. This
232
295
  # predicate returns `false` if reloading is disabled.
233
296
  #
@@ -264,12 +327,15 @@ module Zeitwerk
264
327
  # --- Class methods ---------------------------------------------------------------------------
265
328
 
266
329
  class << self
330
+ include RealModName
331
+
267
332
  # @sig #call | #debug | nil
268
333
  attr_accessor :default_logger
269
334
 
270
335
  # This is a shortcut for
271
336
  #
272
337
  # require "zeitwerk"
338
+ #
273
339
  # loader = Zeitwerk::Loader.new
274
340
  # loader.tag = File.basename(__FILE__, ".rb")
275
341
  # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
@@ -284,7 +350,36 @@ module Zeitwerk
284
350
  # @sig (bool) -> Zeitwerk::GemLoader
285
351
  def for_gem(warn_on_extra_files: true)
286
352
  called_from = caller_locations(1, 1).first.path
287
- Registry.loader_for_gem(called_from, warn_on_extra_files: warn_on_extra_files)
353
+ Registry.loader_for_gem(called_from, namespace: Object, warn_on_extra_files: warn_on_extra_files)
354
+ end
355
+
356
+ # This is a shortcut for
357
+ #
358
+ # require "zeitwerk"
359
+ #
360
+ # loader = Zeitwerk::Loader.new
361
+ # loader.tag = namespace.name + "-" + File.basename(__FILE__, ".rb")
362
+ # loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
363
+ # loader.push_dir(__dir__, namespace: namespace)
364
+ #
365
+ # except that this method returns the same object in subsequent calls from
366
+ # the same file, in the unlikely case the gem wants to be able to reload.
367
+ #
368
+ # This method returns a subclass of Zeitwerk::Loader, but the exact type
369
+ # is private, client code can only rely on the interface.
370
+ #
371
+ # @sig (bool) -> Zeitwerk::GemLoader
372
+ def for_gem_extension(namespace)
373
+ unless namespace.is_a?(Module) # Note that Class < Module.
374
+ raise Zeitwerk::Error, "#{namespace.inspect} is not a class or module object, should be"
375
+ end
376
+
377
+ unless real_mod_name(namespace)
378
+ raise Zeitwerk::Error, "extending anonymous namespaces is unsupported"
379
+ end
380
+
381
+ called_from = caller_locations(1, 1).first.path
382
+ Registry.loader_for_gem(called_from, namespace: namespace, warn_on_extra_files: false)
288
383
  end
289
384
 
290
385
  # Broadcasts `eager_load` to all loaders. Those that have not been setup
@@ -479,7 +574,6 @@ module Zeitwerk
479
574
  raise Error,
480
575
  "loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
481
576
  " which is already managed by\n\n#{loader.pretty_inspect}\n"
482
- EOS
483
577
  end
484
578
  end
485
579
  end
@@ -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.7"
4
+ VERSION = "2.6.9"
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.7
4
+ version: 2.6.9
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-02-10 00:00:00.000000000 Z
11
+ date: 2023-07-25 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.4.3
64
+ rubygems_version: 3.4.16
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: Efficient and thread-safe constant autoloader