zeitwerk 2.5.0.beta6 → 2.5.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74d74bec1f4963cc5d04f037e14723408ae5d1f8c4df16f31f2901402e3230ed
4
- data.tar.gz: 3ebd7f860583cc7a560360b2e766193dc0c74c5cd902c7cb632a614e32dc68e7
3
+ metadata.gz: 1f424f7f7538a469affa93fbe0c24533d146eb8dfe7289f3c5167b36abd0e362
4
+ data.tar.gz: 8aa11ee61f57a965e3fbb705713f6a4dcf0e3586dc8a3e90f35801b8e8ff9e12
5
5
  SHA512:
6
- metadata.gz: f0bec92696285efb5ad1f76094b71a4de5a38a0665b493737c945623042b4d744be2c58f4006ed5c8c4abe0ccb70854c94ba63c3b7eeef1a5ae47086a956668d
7
- data.tar.gz: a1392b3b73dcd85769f793aeca9a9d10cb50c70259d88761574c1e62e201b24f32d7122eda587af6017dea93fca27ce9463e5f0dae7b98523d3edba2c13802eb
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.beta6"
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,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.beta6
4
+ version: 2.5.1
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-10-14 00:00:00.000000000 Z
11
+ date: 2021-10-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem
@@ -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
@@ -55,9 +54,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
55
54
  version: '2.5'
56
55
  required_rubygems_version: !ruby/object:Gem::Requirement
57
56
  requirements:
58
- - - ">"
57
+ - - ">="
59
58
  - !ruby/object:Gem::Version
60
- version: 1.3.1
59
+ version: '0'
61
60
  requirements: []
62
61
  rubygems_version: 3.2.22
63
62
  signing_key:
@@ -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