zeitwerk 2.5.0.beta3 → 2.5.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -8
- data/lib/zeitwerk/autoloads.rb +2 -0
- data/lib/zeitwerk/error.rb +2 -0
- data/lib/zeitwerk/explicit_namespace.rb +2 -0
- data/lib/zeitwerk/kernel.rb +1 -1
- data/lib/zeitwerk/loader/callbacks.rb +2 -0
- data/lib/zeitwerk/loader/config.rb +20 -0
- data/lib/zeitwerk/loader/helpers.rb +2 -0
- data/lib/zeitwerk/loader.rb +2 -0
- data/lib/zeitwerk/real_mod_name.rb +2 -0
- data/lib/zeitwerk/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4012f73de9387546f477e8254668d450c00310666bde922b90a6b2be8d4b9740
|
4
|
+
data.tar.gz: 7c403df9ea53494c2eb1a01b3fb81f70ce1a6488a420728f134c7c32ab4cb444
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2222619050df7f4271a73b2abdc258d541249ed34a75738afb1661ba17460dc2a0c53690f33a85e33b5925e28a6e2d292fa9d12711722261ae517d6d38f7520
|
7
|
+
data.tar.gz: 77679e246700480f751e0bce8373d5fe648a78f41a2b8bd97cc0d9c0bdff52651bd64be59ad56809b2785539fd7439f3b9ad554d9ddb8f668fbca9b187d51a24
|
data/README.md
CHANGED
@@ -30,8 +30,10 @@
|
|
30
30
|
- [Zeitwerk::Inflector](#zeitwerkinflector)
|
31
31
|
- [Zeitwerk::GemInflector](#zeitwerkgeminflector)
|
32
32
|
- [Custom inflector](#custom-inflector)
|
33
|
-
- [
|
34
|
-
|
33
|
+
- [Callbacks](#callbacks)
|
34
|
+
- [The on_setup callback](#the-on_setup-callback)
|
35
|
+
- [The on_load callback](#the-on_load-callback)
|
36
|
+
- [The on_unload callback](#the-on_unload-callback)
|
35
37
|
- [Technical details](#technical-details)
|
36
38
|
- [Logging](#logging)
|
37
39
|
- [Loader tag](#loader-tag)
|
@@ -49,7 +51,7 @@
|
|
49
51
|
- [Supported Ruby versions](#supported-ruby-versions)
|
50
52
|
- [Testing](#testing)
|
51
53
|
- [Motivation](#motivation)
|
52
|
-
- [
|
54
|
+
- [Kernel#require is brittle](#kernelrequire-is-brittle)
|
53
55
|
- [Rails autoloading was brittle](#rails-autoloading-was-brittle)
|
54
56
|
- [Thanks](#thanks)
|
55
57
|
- [License](#license)
|
@@ -159,6 +161,16 @@ class HttpCrawler
|
|
159
161
|
end
|
160
162
|
```
|
161
163
|
|
164
|
+
The first example needs a custom [inflection](https://github.com/fxn/zeitwerk#inflection) rule:
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
loader.inflector.inflect("max_retries" => "MAX_RETRIES")
|
168
|
+
```
|
169
|
+
|
170
|
+
Otherwise, Zeitwerk would expect the file to define `MaxRetries`.
|
171
|
+
|
172
|
+
In the second example, no custom rule is needed.
|
173
|
+
|
162
174
|
<a id="markdown-root-directories-and-root-namespaces" name="root-directories-and-root-namespaces"></a>
|
163
175
|
### Root directories and root namespaces
|
164
176
|
|
@@ -553,8 +565,26 @@ class MyGem::Inflector < Zeitwerk::GemInflector
|
|
553
565
|
end
|
554
566
|
```
|
555
567
|
|
568
|
+
<a id="markdown-callbacks" name="callbacks"></a>
|
569
|
+
### Callbacks
|
570
|
+
|
571
|
+
<a id="markdown-the-on_setup-callback" name="the-on_setup-callback"></a>
|
572
|
+
#### The on_setup callback
|
573
|
+
|
574
|
+
The `on_setup` callback is fired on setup and on each reload:
|
575
|
+
|
576
|
+
```ruby
|
577
|
+
loader.on_setup do
|
578
|
+
# Ready to autoload here.
|
579
|
+
end
|
580
|
+
```
|
581
|
+
|
582
|
+
Multiple `on_setup` callbacks are supported, and they run in order of definition.
|
583
|
+
|
584
|
+
If `setup` was already executed, the callback is fired immediately.
|
585
|
+
|
556
586
|
<a id="markdown-the-on_load-callback" name="the-on_load-callback"></a>
|
557
|
-
|
587
|
+
#### The on_load callback
|
558
588
|
|
559
589
|
The usual place to run something when a file is loaded is the file itself. However, sometimes you'd like to be called, and this is possible with the `on_load` callback.
|
560
590
|
|
@@ -613,7 +643,7 @@ There are use cases for this last catch-all callback, but they are rare. If you
|
|
613
643
|
If both types of callbacks are defined, the specific ones run first.
|
614
644
|
|
615
645
|
<a id="markdown-the-on_unload-callback" name="the-on_unload-callback"></a>
|
616
|
-
|
646
|
+
#### The on_unload callback
|
617
647
|
|
618
648
|
When reloading is enabled, you may occasionally need to execute something before a certain autoloaded class or module is unloaded. The `on_unload` callback allows you to do that.
|
619
649
|
|
@@ -927,8 +957,8 @@ and run `bin/test`.
|
|
927
957
|
<a id="markdown-motivation" name="motivation"></a>
|
928
958
|
## Motivation
|
929
959
|
|
930
|
-
<a id="markdown-
|
931
|
-
###
|
960
|
+
<a id="markdown-kernelrequire-is-brittle" name="kernelrequire-is-brittle"></a>
|
961
|
+
### Kernel#require is brittle
|
932
962
|
|
933
963
|
Since `require` has global side-effects, and there is no static way to verify that you have issued the `require` calls for code that your file depends on, in practice it is very easy to forget some. That introduces bugs that depend on the load order.
|
934
964
|
|
@@ -939,7 +969,7 @@ With Zeitwerk, you just name things following conventions and done. Things are a
|
|
939
969
|
<a id="markdown-rails-autoloading-was-brittle" name="rails-autoloading-was-brittle"></a>
|
940
970
|
### Rails autoloading was brittle
|
941
971
|
|
942
|
-
Autoloading in Rails was based on `const_missing` up to Rails 5. That callback lacks fundamental information like the nesting or the resolution algorithm being used. Because of that, Rails autoloading was not able to match Ruby's semantics, and that introduced a series of issues. Zeitwerk is based on a different technique and fixed Rails autoloading starting with Rails 6.
|
972
|
+
Autoloading in Rails was based on `const_missing` up to Rails 5. That callback lacks fundamental information like the nesting or the resolution algorithm being used. Because of that, Rails autoloading was not able to match Ruby's semantics, and that introduced a [series of issues](https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#common-gotchas). Zeitwerk is based on a different technique and fixed Rails autoloading starting with Rails 6.
|
943
973
|
|
944
974
|
<a id="markdown-thanks" name="thanks"></a>
|
945
975
|
## Thanks
|
data/lib/zeitwerk/autoloads.rb
CHANGED
data/lib/zeitwerk/error.rb
CHANGED
data/lib/zeitwerk/kernel.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
module Kernel
|
4
4
|
module_function
|
5
5
|
|
6
|
-
# We are going to decorate
|
6
|
+
# We are going to decorate Kernel#require with two goals.
|
7
7
|
#
|
8
8
|
# First, by intercepting Kernel#require calls, we are able to autovivify
|
9
9
|
# modules on required directories, and also do internal housekeeping when
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "set"
|
2
4
|
require "securerandom"
|
3
5
|
|
@@ -54,6 +56,12 @@ module Zeitwerk::Loader::Config
|
|
54
56
|
# @sig Set[String]
|
55
57
|
attr_reader :eager_load_exclusions
|
56
58
|
|
59
|
+
# User-oriented callbacks to be fired on setup and on reload.
|
60
|
+
#
|
61
|
+
# @private
|
62
|
+
# @sig Array[{ () -> void }]
|
63
|
+
attr_reader :on_setup_callbacks
|
64
|
+
|
57
65
|
# User-oriented callbacks to be fired when a constant is loaded.
|
58
66
|
#
|
59
67
|
# @private
|
@@ -81,6 +89,7 @@ module Zeitwerk::Loader::Config
|
|
81
89
|
@collapse_dirs = Set.new
|
82
90
|
@eager_load_exclusions = Set.new
|
83
91
|
@reloading_enabled = false
|
92
|
+
@on_setup_callbacks = []
|
84
93
|
@on_load_callbacks = {}
|
85
94
|
@on_unload_callbacks = {}
|
86
95
|
@logger = self.class.default_logger
|
@@ -188,6 +197,17 @@ module Zeitwerk::Loader::Config
|
|
188
197
|
end
|
189
198
|
end
|
190
199
|
|
200
|
+
# Configure a block to be called after setup and on each reload.
|
201
|
+
# If setup was already done, the block runs immediately.
|
202
|
+
#
|
203
|
+
# @sig () { () -> void } -> void
|
204
|
+
def on_setup(&block)
|
205
|
+
mutex.synchronize do
|
206
|
+
on_setup_callbacks << block
|
207
|
+
block.call if @setup
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
191
211
|
# Configure a block to be invoked once a certain constant path is loaded.
|
192
212
|
# Supports multiple callbacks, and if there are many, they are executed in
|
193
213
|
# the order in which they were defined.
|
data/lib/zeitwerk/loader.rb
CHANGED
data/lib/zeitwerk/version.rb
CHANGED
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.5.0.
|
4
|
+
version: 2.5.0.beta4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Noria
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-09-
|
11
|
+
date: 2021-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Zeitwerk implements constant autoloading with Ruby semantics. Each gem
|