zeitwerk 1.3.0 → 1.3.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: 69ab50936501a8a78e6be01014ac8e97890297955088fdac78cf1d5adeea7e78
4
- data.tar.gz: 2afe0ae004e102604e2b23133ef683bad6753479bd9be0ef37379676d5c47a09
3
+ metadata.gz: 172e4bb51ab526d5f4f606f0c4099cd9d4b8bdfe15a2ec851ed58e7095018f07
4
+ data.tar.gz: 74b2d3f010de6374d919e7cae4d759a86490d91bfb7cd3f743b314e10569d2e9
5
5
  SHA512:
6
- metadata.gz: 51ceaebc945976ba34a6025d225099ab69e337287a3577c1f1e484d0fc1e4d6efec00e2e132a9cb794d91101f1f484dc154ef6e97c3b0a392f16a9ec5c340bdb
7
- data.tar.gz: ab59de0733cdbb0496d75ddb72ffbb744a4258ca7bca73ad0817312a07ac0e715d490c906cc24307dfb719bf3a4ac7cbf7fd9396c98ba66b224789dd987830d1
6
+ metadata.gz: 57531ede94526489ffb47235b0113ff6cb290c5d21c4b03dbbdea79a55b0e6b74d685b8f5d57f5b0bf9ec85986e731368bd57b9f5dd0ccfdc40033e5b570bbaa
7
+ data.tar.gz: d50a101226b04214af4bcc6961422501044a4b9bede6d18bc06a622736bedd7d19fa9b8b9191b8f08042382d0bebcfb8e0162aa7aef37eddd2e17dbcdc6ee8a6
@@ -14,17 +14,17 @@ module Kernel
14
14
  if loader = Zeitwerk::Registry.loader_for(path)
15
15
  if path.end_with?(".rb")
16
16
  zeitwerk_original_require(path).tap do |required|
17
- loader.on_file_loaded(path) if required
17
+ loader.on_file_autoloaded(path) if required
18
18
  end
19
19
  else
20
- loader.on_dir_loaded(path)
20
+ loader.on_dir_autoloaded(path)
21
21
  end
22
22
  else
23
23
  zeitwerk_original_require(path).tap do |required|
24
24
  if required
25
25
  realpath = $LOADED_FEATURES.last
26
26
  if loader = Zeitwerk::Registry.loader_for(realpath)
27
- loader.on_file_loaded(realpath)
27
+ loader.on_file_autoloaded(realpath)
28
28
  end
29
29
  end
30
30
  end
@@ -5,6 +5,9 @@ require "securerandom"
5
5
 
6
6
  module Zeitwerk
7
7
  class Loader
8
+ require_relative "loader/callbacks"
9
+ include Callbacks
10
+
8
11
  # @return [String]
9
12
  attr_reader :tag
10
13
 
@@ -117,11 +120,7 @@ module Zeitwerk
117
120
  @eager_loaded = false
118
121
 
119
122
  @tracer = TracePoint.new(:class) do |tp|
120
- unless lazy_subdirs.empty? # do not even compute the hash key if not needed
121
- if subdirs = lazy_subdirs.delete(tp.self.name)
122
- subdirs.each { |subdir| set_autoloads_in_dir(subdir, tp.self) }
123
- end
124
- end
123
+ on_namespace_loaded(tp.self)
125
124
  end
126
125
 
127
126
  Registry.register_loader(self)
@@ -147,14 +146,12 @@ module Zeitwerk
147
146
  # @param path [<String, Pathname>]
148
147
  # @return [void]
149
148
  def push_dir(path)
150
- self.class.mutex.synchronize do
151
- abspath = File.expand_path(path)
152
- if dir?(abspath)
153
- raise_if_conflicting_directory(abspath)
154
- root_dirs[abspath] = true
155
- else
156
- raise ArgumentError, "the root directory #{abspath} does not exist"
157
- end
149
+ abspath = File.expand_path(path)
150
+ if dir?(abspath)
151
+ raise_if_conflicting_directory(abspath)
152
+ root_dirs[abspath] = true
153
+ else
154
+ raise ArgumentError, "the root directory #{abspath} does not exist"
158
155
  end
159
156
  end
160
157
 
@@ -192,12 +189,13 @@ module Zeitwerk
192
189
  # @return [void]
193
190
  def setup
194
191
  mutex.synchronize do
195
- unless @setup
196
- expand_ignored_glob_patterns
197
- non_ignored_root_dirs.each { |root_dir| set_autoloads_in_dir(root_dir, Object) }
198
- do_preload
199
- @setup = true
200
- end
192
+ break if @setup
193
+
194
+ expand_ignored_glob_patterns
195
+ non_ignored_root_dirs.each { |root_dir| set_autoloads_in_dir(root_dir, Object) }
196
+ do_preload
197
+
198
+ @setup = true
201
199
  end
202
200
  end
203
201
 
@@ -226,6 +224,7 @@ module Zeitwerk
226
224
  # array lookups, since directories are not stored in $LOADED_FEATURES.
227
225
  $LOADED_FEATURES.delete(path) if ruby?(path)
228
226
  end
227
+
229
228
  autoloads.clear
230
229
  loaded.clear
231
230
  lazy_subdirs.clear
@@ -274,7 +273,7 @@ module Zeitwerk
274
273
  end
275
274
  end
276
275
 
277
- # Says if the given constant path as been loaded.
276
+ # Says if the given constant path has been loaded.
278
277
  #
279
278
  # @param cpath [String]
280
279
  # @return [Boolean]
@@ -285,7 +284,7 @@ module Zeitwerk
285
284
  # --- Class methods ---------------------------------------------------------------------------
286
285
 
287
286
  class << self
288
- # @return [#call, nil]
287
+ # @return [#call, #debug, nil]
289
288
  attr_accessor :default_logger
290
289
 
291
290
  # @private
@@ -296,6 +295,7 @@ module Zeitwerk
296
295
  #
297
296
  # require "zeitwerk"
298
297
  # loader = Zeitwerk::Loader.new
298
+ # loader.tag = File.basename(__FILE__, ".rb")
299
299
  # loader.inflector = Zeitwerk::GemInflector.new
300
300
  # loader.push_dir(__dir__)
301
301
  #
@@ -326,37 +326,6 @@ module Zeitwerk
326
326
 
327
327
  self.mutex = Mutex.new
328
328
 
329
- # --- Callbacks -------------------------------------------------------------------------------
330
-
331
- # Callback invoked from Kernel when a managed file is loaded.
332
- #
333
- # @private
334
- # @param file [String]
335
- # @return [void]
336
- def on_file_loaded(file)
337
- parent, cname = autoloads[file]
338
- loaded.add(cpath(parent, cname))
339
- log("constant #{cpath(parent, cname)} loaded from file #{file}") if logger
340
- end
341
-
342
- # Callback invoked from Kernel when a managed directory is loaded.
343
- #
344
- # @private
345
- # @param dir [String]
346
- # @return [void]
347
- def on_dir_loaded(dir)
348
- parent, cname = autoloads[dir]
349
-
350
- autovivified_module = parent.const_set(cname, Module.new)
351
- log("module #{autovivified_module.name} autovivified from directory #{dir}") if logger
352
-
353
- loaded.add(autovivified_module.name)
354
-
355
- if subdirs = lazy_subdirs[autovivified_module.name]
356
- subdirs.each { |subdir| set_autoloads_in_dir(subdir, autovivified_module) }
357
- end
358
- end
359
-
360
329
  private # -------------------------------------------------------------------------------------
361
330
 
362
331
  # @return [<String>]
@@ -554,16 +523,18 @@ module Zeitwerk
554
523
  end
555
524
 
556
525
  def raise_if_conflicting_directory(dir)
557
- Registry.loaders.each do |loader|
558
- next if loader == self
559
-
560
- loader.dirs.each do |already_managed_dir|
561
- if dir.start_with?(already_managed_dir) || already_managed_dir.start_with?(dir)
562
- require "pp"
563
- raise ConflictingDirectory,
564
- "loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
565
- " which is already managed by\n\n#{loader.pretty_inspect}\n"
566
- EOS
526
+ self.class.mutex.synchronize do
527
+ Registry.loaders.each do |loader|
528
+ next if loader == self
529
+
530
+ loader.dirs.each do |already_managed_dir|
531
+ if dir.start_with?(already_managed_dir) || already_managed_dir.start_with?(dir)
532
+ require "pp"
533
+ raise ConflictingDirectory,
534
+ "loader\n\n#{pretty_inspect}\n\nwants to manage directory #{dir}," \
535
+ " which is already managed by\n\n#{loader.pretty_inspect}\n"
536
+ EOS
537
+ end
567
538
  end
568
539
  end
569
540
  end
@@ -0,0 +1,43 @@
1
+ module Zeitwerk::Loader::Callbacks
2
+ # Invoked from our decorated Kernel#require when a managed file is autoloaded.
3
+ #
4
+ # @private
5
+ # @param file [String]
6
+ # @return [void]
7
+ def on_file_autoloaded(file)
8
+ parent, cname = autoloads[file]
9
+ loaded.add(cpath(parent, cname))
10
+ log("constant #{cpath(parent, cname)} loaded from file #{file}") if logger
11
+ end
12
+
13
+ # Invoked from our decorated Kernel#require when a managed directory is
14
+ # autoloaded.
15
+ #
16
+ # @private
17
+ # @param dir [String]
18
+ # @return [void]
19
+ def on_dir_autoloaded(dir)
20
+ parent, cname = autoloads[dir]
21
+
22
+ autovivified_module = parent.const_set(cname, Module.new)
23
+ log("module #{autovivified_module.name} autovivified from directory #{dir}") if logger
24
+
25
+ loaded.add(autovivified_module.name)
26
+ on_namespace_loaded(autovivified_module)
27
+ end
28
+
29
+ # Invoked when a class or module is created or reopened, either from the
30
+ # tracer or from module autovivification. If the namespace has matching
31
+ # subdirectories, we descend into them now.
32
+ #
33
+ # @private
34
+ # @param namespace [Module]
35
+ # @return [void]
36
+ def on_namespace_loaded(namespace)
37
+ if subdirs = lazy_subdirs.delete(namespace.name)
38
+ subdirs.each do |subdir|
39
+ set_autoloads_in_dir(subdir, namespace)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "1.3.0"
4
+ VERSION = "1.3.1"
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: 1.3.0
4
+ version: 1.3.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: 2019-02-21 00:00:00.000000000 Z
11
+ date: 2019-02-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem
@@ -27,6 +27,7 @@ files:
27
27
  - lib/zeitwerk/inflector.rb
28
28
  - lib/zeitwerk/kernel.rb
29
29
  - lib/zeitwerk/loader.rb
30
+ - lib/zeitwerk/loader/callbacks.rb
30
31
  - lib/zeitwerk/registry.rb
31
32
  - lib/zeitwerk/version.rb
32
33
  homepage: https://github.com/fxn/zeitwerk