zeitwerk 2.5.0 → 2.5.1

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: c85bb1028371436b80651c897670fef7158dcca7a916b10f6cb8584684c44442
4
- data.tar.gz: c07fec548ab787daa28b10260beb022d7375aff90a506daa9bfecdc2f5bd6e5c
3
+ metadata.gz: 1f424f7f7538a469affa93fbe0c24533d146eb8dfe7289f3c5167b36abd0e362
4
+ data.tar.gz: 8aa11ee61f57a965e3fbb705713f6a4dcf0e3586dc8a3e90f35801b8e8ff9e12
5
5
  SHA512:
6
- metadata.gz: d01e5091fa868cdc80c4dd6eb82cc3ace4e21e0704aa2128e0ef5098a52140884daa62e9da862490104e31d393e06b33e29170e6d3f6fd3e44aa5111b25a06f1
7
- data.tar.gz: 055d861fe2440f465277fbfce4c2bd9b7460d38f14dc5435449c6655d1be97f7e24fb7154b64102267c020e45d52f0bff21fbc7fc35eec0063b2dac2a92197bd
6
+ metadata.gz: 70022bbaa975835042e6550a3e1f8ca8e09e7e1f764d29c659adc9d6c727765fea276da4425ae43e7f4baaaaae6f167d9f4c567e63c0d322ac553603f2d6063b
7
+ data.tar.gz: 7144f057043a326be20b866b2a23dc3c2b5cd3274a084a73770e6b643f65e308ba2eacfbf87701092ee20d440e256d0afe37066933d396cb332d28e5108775a7
@@ -14,22 +14,16 @@ module Zeitwerk
14
14
  include Helpers
15
15
  include Config
16
16
 
17
- # Keeps track of autoloads defined by the loader which have not been
18
- # executed so far.
17
+ # Maps absolute paths for which an autoload has been set ---and not
18
+ # executed--- to their corresponding parent class or module and constant
19
+ # name.
19
20
  #
20
- # This metadata helps us implement a few things:
21
- #
22
- # 1. When autoloads are triggered, ensure they define the expected constant
23
- # and invoke user callbacks. If reloading is enabled, remember cref and
24
- # abspath for later unloading logic.
25
- #
26
- # 2. When unloading, remove autoloads that have not been executed.
27
- #
28
- # 3. Eager load with a recursive const_get, rather than a recursive require,
29
- # for consistency with lazy loading.
21
+ # "/Users/fxn/blog/app/models/user.rb" => [Object, :User],
22
+ # "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
23
+ # ...
30
24
  #
31
25
  # @private
32
- # @sig Zeitwerk::Autoloads
26
+ # @sig Hash[String, [Module, Symbol]]
33
27
  attr_reader :autoloads
34
28
 
35
29
  # We keep track of autoloaded directories to remove them from the registry
@@ -87,7 +81,7 @@ module Zeitwerk
87
81
  def initialize
88
82
  super
89
83
 
90
- @autoloads = Autoloads.new
84
+ @autoloads = {}
91
85
  @autoloaded_dirs = []
92
86
  @to_unload = {}
93
87
  @lazy_subdirs = Hash.new { |h, cpath| h[cpath] = [] }
@@ -138,7 +132,7 @@ module Zeitwerk
138
132
  # is enough.
139
133
  unloaded_files = Set.new
140
134
 
141
- autoloads.each do |(parent, cname), abspath|
135
+ autoloads.each do |abspath, (parent, cname)|
142
136
  if parent.autoload?(cname)
143
137
  unload_autoload(parent, cname)
144
138
  else
@@ -234,7 +228,7 @@ module Zeitwerk
234
228
  next if honour_exclusions && excluded_from_eager_load?(abspath)
235
229
 
236
230
  if ruby?(abspath)
237
- if cref = autoloads.cref_for(abspath)
231
+ if cref = autoloads[abspath]
238
232
  cget(*cref)
239
233
  end
240
234
  elsif dir?(abspath) && !root_dirs.key?(abspath)
@@ -376,7 +370,7 @@ module Zeitwerk
376
370
 
377
371
  # @sig (Module, Symbol, String) -> void
378
372
  def autoload_subdir(parent, cname, subdir)
379
- if autoload_path = autoloads.abspath_for(parent, cname)
373
+ if autoload_path = autoload_path_set_by_me_for?(parent, cname)
380
374
  cpath = cpath(parent, cname)
381
375
  register_explicit_namespace(cpath) if ruby?(autoload_path)
382
376
  # We do not need to issue another autoload, the existing one is enough
@@ -432,7 +426,7 @@ module Zeitwerk
432
426
 
433
427
  # @sig (Module, Symbol, String) -> void
434
428
  def set_autoload(parent, cname, abspath)
435
- autoloads.define(parent, cname, abspath)
429
+ parent.autoload(cname, abspath)
436
430
 
437
431
  if logger
438
432
  if ruby?(abspath)
@@ -442,6 +436,7 @@ module Zeitwerk
442
436
  end
443
437
  end
444
438
 
439
+ autoloads[abspath] = [parent, cname]
445
440
  Registry.register_autoload(self, abspath)
446
441
 
447
442
  # See why in the documentation of Zeitwerk::Registry.inceptions.
@@ -450,6 +445,15 @@ module Zeitwerk
450
445
  end
451
446
  end
452
447
 
448
+ # @sig (Module, Symbol) -> String?
449
+ def autoload_path_set_by_me_for?(parent, cname)
450
+ if autoload_path = strict_autoload_path(parent, cname)
451
+ autoload_path if autoloads.key?(autoload_path)
452
+ else
453
+ Registry.inception?(cpath(parent, cname))
454
+ end
455
+ end
456
+
453
457
  # @sig (String) -> void
454
458
  def register_explicit_namespace(cpath)
455
459
  ExplicitNamespace.register(cpath, self)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.5.0"
4
+ VERSION = "2.5.1"
5
5
  end
data/lib/zeitwerk.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  module Zeitwerk
4
4
  require_relative "zeitwerk/real_mod_name"
5
5
  require_relative "zeitwerk/loader"
6
- require_relative "zeitwerk/autoloads"
7
6
  require_relative "zeitwerk/registry"
8
7
  require_relative "zeitwerk/explicit_namespace"
9
8
  require_relative "zeitwerk/inflector"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zeitwerk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
@@ -23,7 +23,6 @@ files:
23
23
  - MIT-LICENSE
24
24
  - README.md
25
25
  - lib/zeitwerk.rb
26
- - lib/zeitwerk/autoloads.rb
27
26
  - lib/zeitwerk/error.rb
28
27
  - lib/zeitwerk/explicit_namespace.rb
29
28
  - lib/zeitwerk/gem_inflector.rb
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Zeitwerk
4
- # @private
5
- class Autoloads
6
- # Maps crefs for which an autoload has been defined to the corresponding
7
- # absolute path.
8
- #
9
- # [Object, :User] => "/Users/fxn/blog/app/models/user.rb"
10
- # [Object, :Hotel] => "/Users/fxn/blog/app/models/hotel"
11
- # ...
12
- #
13
- # This colection is transient, callbacks delete its entries as autoloads get
14
- # executed.
15
- #
16
- # @sig Hash[[Module, Symbol], String]
17
- attr_reader :c2a
18
-
19
- # This is the inverse of c2a, for inverse lookups.
20
- #
21
- # @sig Hash[String, [Module, Symbol]]
22
- attr_reader :a2c
23
-
24
- # @sig () -> void
25
- def initialize
26
- @c2a = {}
27
- @a2c = {}
28
- end
29
-
30
- # @sig (Module, Symbol, String) -> void
31
- def define(parent, cname, abspath)
32
- parent.autoload(cname, abspath)
33
- cref = [parent, cname]
34
- c2a[cref] = abspath
35
- a2c[abspath] = cref
36
- end
37
-
38
- # @sig () { () -> [[Module, Symbol], String] } -> void
39
- def each(&block)
40
- c2a.each(&block)
41
- end
42
-
43
- # @sig (Module, Symbol) -> String?
44
- def abspath_for(parent, cname)
45
- c2a[[parent, cname]]
46
- end
47
-
48
- # @sig (String) -> [Module, Symbol]?
49
- def cref_for(abspath)
50
- a2c[abspath]
51
- end
52
-
53
- # @sig (String) -> [Module, Symbol]?
54
- def delete(abspath)
55
- cref = a2c.delete(abspath)
56
- c2a.delete(cref)
57
- cref
58
- end
59
-
60
- # @sig () -> void
61
- def clear
62
- c2a.clear
63
- a2c.clear
64
- end
65
-
66
- # @sig () -> bool
67
- def empty?
68
- c2a.empty? && a2c.empty?
69
- end
70
- end
71
- end