zeitwerk 2.1.2 → 2.1.3
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 +4 -4
- data/lib/zeitwerk/inflector.rb +4 -3
- data/lib/zeitwerk/loader.rb +33 -28
- 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: e458722eb922aa69f1c49c077b9cfa0bb8c0fa14d7a12f295b7375e5f31d6d33
|
4
|
+
data.tar.gz: 414f3bbe461b6081c39eff4640fda5eae0ea7d2e1649a981f84759ba6db538a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc0a61c385d372cdba449e80473d793133e71fc502cbda1f504d64759f6ff9e1ea65ebdcead21c472b89c84951915d687de2a3e71bff8307b91e2e4bd29339d5
|
7
|
+
data.tar.gz: 5b851f501b91740176047eb1fca04c3b5de04c482b055dc7605a15cbf571b719e4bb9e11bc939521411e1965ef07e539a023dc83b40eb059234b86fe41bf73d5
|
data/lib/zeitwerk/inflector.rb
CHANGED
@@ -4,9 +4,10 @@ module Zeitwerk
|
|
4
4
|
class Inflector # :nodoc:
|
5
5
|
# Very basic snake case -> camel case conversion.
|
6
6
|
#
|
7
|
-
# Zeitwerk::Inflector.
|
8
|
-
#
|
9
|
-
#
|
7
|
+
# inflector = Zeitwerk::Inflector.new
|
8
|
+
# inflector.camelize("post", ...) # => "Post"
|
9
|
+
# inflector.camelize("users_controller", ...) # => "UsersController"
|
10
|
+
# inflector.camelize("api", ...) # => "Api"
|
10
11
|
#
|
11
12
|
# @param basename [String]
|
12
13
|
# @param _abspath [String]
|
data/lib/zeitwerk/loader.rb
CHANGED
@@ -74,12 +74,12 @@ module Zeitwerk
|
|
74
74
|
# executed--- to their corresponding parent class or module and constant
|
75
75
|
# name.
|
76
76
|
#
|
77
|
-
# "/Users/fxn/blog/app/models/user.rb" => [Object,
|
78
|
-
# "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel,
|
77
|
+
# "/Users/fxn/blog/app/models/user.rb" => [Object, :User],
|
78
|
+
# "/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
|
79
79
|
# ...
|
80
80
|
#
|
81
81
|
# @private
|
82
|
-
# @return [{String => (Module,
|
82
|
+
# @return [{String => (Module, Symbol)}]
|
83
83
|
attr_reader :autoloads
|
84
84
|
|
85
85
|
# We keep track of autoloaded directories to remove them from the registry
|
@@ -92,12 +92,20 @@ module Zeitwerk
|
|
92
92
|
# @return [<String>]
|
93
93
|
attr_reader :autoloaded_dirs
|
94
94
|
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
95
|
+
# Stores metadata needed for unloading. Its entries look like this:
|
96
|
+
#
|
97
|
+
# "Admin::Role" => [".../admin/role.rb", [Admin, :Role]]
|
98
|
+
#
|
99
|
+
# The constant path as key helps implementing to_unload? The real file name
|
100
|
+
# is stored in order to be able to delete it from $LOADED_FEATURES, and the
|
101
|
+
# pair [Module, Symbol] is used to remove_const the constant from the class
|
102
|
+
# or module object.
|
103
|
+
#
|
104
|
+
# If reloading is enabled, this hash is filled as constants are autoloaded
|
105
|
+
# or eager loaded. Otherwise, the collection remains empty.
|
98
106
|
#
|
99
107
|
# @private
|
100
|
-
# @return [{String => (String, (Module,
|
108
|
+
# @return [{String => (String, (Module, Symbol))}]
|
101
109
|
attr_reader :to_unload
|
102
110
|
|
103
111
|
# Maps constant paths of namespaces to arrays of corresponding directories.
|
@@ -176,7 +184,11 @@ module Zeitwerk
|
|
176
184
|
root_dirs.keys.freeze
|
177
185
|
end
|
178
186
|
|
179
|
-
# Pushes `
|
187
|
+
# Pushes `path` to the list of root directories.
|
188
|
+
#
|
189
|
+
# Raises `Zeitwerk::Error` if `path` does not exist, or if another loader in
|
190
|
+
# the same process already manages that directory or one of its ascendants
|
191
|
+
# or descendants.
|
180
192
|
#
|
181
193
|
# @param path [<String, Pathname>]
|
182
194
|
# @raise [Zeitwerk::Error]
|
@@ -413,7 +425,7 @@ module Zeitwerk
|
|
413
425
|
#
|
414
426
|
# @return [Zeitwerk::Loader]
|
415
427
|
def for_gem
|
416
|
-
called_from =
|
428
|
+
called_from = caller_locations.first.path
|
417
429
|
Registry.loader_for_gem(called_from)
|
418
430
|
end
|
419
431
|
|
@@ -447,7 +459,7 @@ module Zeitwerk
|
|
447
459
|
# @return [void]
|
448
460
|
def set_autoloads_in_dir(dir, parent)
|
449
461
|
each_abspath(dir) do |abspath|
|
450
|
-
cname = inflector.camelize(File.basename(abspath, ".rb"), abspath)
|
462
|
+
cname = inflector.camelize(File.basename(abspath, ".rb"), abspath).to_sym
|
451
463
|
if ruby?(abspath)
|
452
464
|
autoload_file(parent, cname, abspath)
|
453
465
|
elsif dir?(abspath)
|
@@ -463,7 +475,7 @@ module Zeitwerk
|
|
463
475
|
end
|
464
476
|
|
465
477
|
# @param parent [Module]
|
466
|
-
# @param cname [
|
478
|
+
# @param cname [Symbol]
|
467
479
|
# @param subdir [String]
|
468
480
|
# @return [void]
|
469
481
|
def autoload_subdir(parent, cname, subdir)
|
@@ -486,7 +498,7 @@ module Zeitwerk
|
|
486
498
|
end
|
487
499
|
|
488
500
|
# @param parent [Module]
|
489
|
-
# @param cname [
|
501
|
+
# @param cname [Symbol]
|
490
502
|
# @param file [String]
|
491
503
|
# @return [void]
|
492
504
|
def autoload_file(parent, cname, file)
|
@@ -514,7 +526,7 @@ module Zeitwerk
|
|
514
526
|
# @param dir [String] directory that would have autovivified a module
|
515
527
|
# @param file [String] the file where the namespace is explictly defined
|
516
528
|
# @param parent [Module]
|
517
|
-
# @param cname [
|
529
|
+
# @param cname [Symbol]
|
518
530
|
# @return [void]
|
519
531
|
def promote_namespace_from_implicit_to_explicit(dir:, file:, parent:, cname:)
|
520
532
|
autoloads.delete(dir)
|
@@ -525,7 +537,7 @@ module Zeitwerk
|
|
525
537
|
end
|
526
538
|
|
527
539
|
# @param parent [Module]
|
528
|
-
# @param cname [
|
540
|
+
# @param cname [Symbol]
|
529
541
|
# @param abspath [String]
|
530
542
|
# @return [void]
|
531
543
|
def set_autoload(parent, cname, abspath)
|
@@ -552,7 +564,7 @@ module Zeitwerk
|
|
552
564
|
end
|
553
565
|
|
554
566
|
# @param parent [Module]
|
555
|
-
# @param cname [
|
567
|
+
# @param cname [Symbol]
|
556
568
|
# @return [String, nil]
|
557
569
|
def autoload_for?(parent, cname)
|
558
570
|
strict_autoload_path(parent, cname) || Registry.inception?(cpath(parent, cname))
|
@@ -575,17 +587,10 @@ module Zeitwerk
|
|
575
587
|
# We need a way to strictly check in parent ignoring ancestors.
|
576
588
|
#
|
577
589
|
# @param parent [Module]
|
578
|
-
# @param cname [
|
590
|
+
# @param cname [Symbol]
|
579
591
|
# @return [String, nil]
|
580
592
|
def strict_autoload_path(parent, cname)
|
581
|
-
|
582
|
-
# Due to the use cases we have, we are done if parent is a Module.
|
583
|
-
return autoload_path unless parent.is_a?(Class)
|
584
|
-
# Since file and constant names match, if both parent and one of its
|
585
|
-
# ancestors have an autoload for the same cname, their autoload paths
|
586
|
-
# cannot be equal.
|
587
|
-
return autoload_path unless parent.superclass.autoload?(cname) == autoload_path
|
588
|
-
end
|
593
|
+
parent.autoload?(cname) if cdef?(parent, cname)
|
589
594
|
end
|
590
595
|
|
591
596
|
# This method is called this way because I prefer `preload` to be the method
|
@@ -624,10 +629,10 @@ module Zeitwerk
|
|
624
629
|
end
|
625
630
|
|
626
631
|
# @param parent [Module]
|
627
|
-
# @param cname [
|
632
|
+
# @param cname [Symbol]
|
628
633
|
# @return [String]
|
629
634
|
def cpath(parent, cname)
|
630
|
-
parent.equal?(Object) ? cname : "#{parent.name}::#{cname}"
|
635
|
+
parent.equal?(Object) ? cname.to_s : "#{parent.name}::#{cname}"
|
631
636
|
end
|
632
637
|
|
633
638
|
# @param dir [String]
|
@@ -693,7 +698,7 @@ module Zeitwerk
|
|
693
698
|
end
|
694
699
|
|
695
700
|
# @param parent [Module]
|
696
|
-
# @param cname [
|
701
|
+
# @param cname [Symbol]
|
697
702
|
# @return [void]
|
698
703
|
def unload_autoload(parent, cname)
|
699
704
|
parent.send(:remove_const, cname)
|
@@ -701,7 +706,7 @@ module Zeitwerk
|
|
701
706
|
end
|
702
707
|
|
703
708
|
# @param parent [Module]
|
704
|
-
# @param cname [
|
709
|
+
# @param cname [Symbol]
|
705
710
|
# @return [void]
|
706
711
|
def unload_cref(parent, cname)
|
707
712
|
parent.send(:remove_const, cname)
|
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.1.
|
4
|
+
version: 2.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xavier Noria
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |2
|
14
14
|
Zeitwerk implements constant autoloading with Ruby semantics. Each gem
|