zeitwerk 1.0.0.beta2 → 1.0.0.beta3

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: cfc51c080dd7eb9b63e869a04b6eeeb14c0098b64de32d0ef872bba22a6d85d2
4
- data.tar.gz: 0d1f743c91aefa0dd073f8f9d05c97991849cd5aa2552633048c15e93269015c
3
+ metadata.gz: 5a765d51697ca7909b3c4f5ff379c5bda9f15c83ecaeda5becd3f1a3ff8399a4
4
+ data.tar.gz: 9e156a434cb867d0abd4b5e2863b389b80f8112a77be753931d6f935e69a4bba
5
5
  SHA512:
6
- metadata.gz: 05fba3abfe897598b45650dd837bb8e7fe4ae19f8ddd43bac1e934e2da18d1374bec14c13c46e43ad37f5cc31f991ddd9c9e429455aa7014bc4e6760b690a03d
7
- data.tar.gz: 9ae4f99de1c0e2ace6be146c80fb7384cc893f57f2ec4e6970b44b85ce80d9cf2aa95300b2aac18cb87bc56dfd07c3d155b6b7898c21216b303a1ebc24575414
6
+ metadata.gz: 7b207559245f9b6c89dedb37bb36395fbf766cca8a947f841b2017d745ddd924edabe42c1708ee2d02e07ea1ce9017f0cbfb09ce97f8cfe939606c137b8fc35a
7
+ data.tar.gz: 24a76c3eab195b90b496c69c915105128d658c696122aeb8c0ffda47bfa33432273a53740eb889d4309cac05d1092a2053d2019c98dd37c8f9a629a0d17598a1
data/README.md CHANGED
@@ -23,6 +23,9 @@
23
23
  - [Custom inflector](#custom-inflector)
24
24
  - [Logging](#logging)
25
25
  - [Ignoring parts of the project](#ignoring-parts-of-the-project)
26
+ - [Files that do not follow the conventions](#files-that-do-not-follow-the-conventions)
27
+ - [The adapter pattern](#the-adapter-pattern)
28
+ - [Test files mixed with implementation files](#test-files-mixed-with-implementation-files)
26
29
  - [Edge cases](#edge-cases)
27
30
  - [Pronunciation](#pronunciation)
28
31
  - [Supported Ruby versions](#supported-ruby-versions)
@@ -296,7 +299,17 @@ If your project has namespaces, you'll notice in the traces Zeitwerk sets autolo
296
299
 
297
300
  ### Ignoring parts of the project
298
301
 
299
- Sometimes it might be convenient to tell Zeitwerk to completely ignore some particular file or directory. For example, let's suppose that your gem decorates something in `Kernel`:
302
+ Zeitwerk ignores automatically any file or directory whose name starts with a dot, and any files that do not have extension ".rb".
303
+
304
+ However, sometimes it might still be convenient to tell Zeitwerk to completely ignore some particular Ruby file or directory. That is possible with `ignore`, which accepts an arbitrary number of strings or `Pathname` objects, and also an array of them.
305
+
306
+ You can ignore file names, directory names, and glob patterns. Glob patterns are expanded on setup and on reload.
307
+
308
+ Let's see some use cases.
309
+
310
+ #### Files that do not follow the conventions
311
+
312
+ Let's suppose that your gem decorates something in `Kernel`:
300
313
 
301
314
  ```ruby
302
315
  # lib/my_gem/core_ext/kernel.rb
@@ -306,20 +319,20 @@ Kernel.module_eval do
306
319
  end
307
320
  ```
308
321
 
309
- That file does not follow the conventions and you need to tell Zeitwerk:
322
+ That file does not define a constant path after the path name and you need to tell Zeitwerk:
310
323
 
311
324
  ```ruby
312
- kernel_ext = File.expand_path("my_gem/core_ext/kernel.rb", __dir__)
325
+ kernel_ext = "#{__dir__}/my_gem/core_ext/kernel.rb"
313
326
  loader.ignore(kernel_ext)
314
327
  loader.setup
315
328
  ```
316
329
 
317
- You can pass several arguments to this method, also an array of strings. And you can call `ignore` multiple times too.
330
+ #### The adapter pattern
318
331
 
319
332
  Another use case for ignoring files is the adapter pattern. Let's imagine your project talks to databases, supports several, and has adapters for each one of them. Those adapters may have top-level `require` calls that load their respective drivers, but you don't want your users to install them all, only the one they are going to use. On the other hand, if your code is eager loaded by you or a parent project (with `Zeitwerk::Loader.eager_load_all`), those `require` calls are going to be executed. Ignoring the adapters prevents that:
320
333
 
321
334
  ```ruby
322
- db_adapters = File.expand_path("my_gem/db_adapters", __dir__)
335
+ db_adapters = "#{__dir__}/my_gem/db_adapters"
323
336
  loader.ignore(db_adapters)
324
337
  loader.setup
325
338
  ```
@@ -330,6 +343,16 @@ The chosen adapter, then, has to be loaded by hand somehow:
330
343
  require "my_gem/db_adapters/#{config[:db_adapter]}"
331
344
  ```
332
345
 
346
+ #### Test files mixed with implementation files
347
+
348
+ There are project layouts that put implementation files and test files together. To ignore the test files, you can use a glob pattern like this:
349
+
350
+ ```ruby
351
+ tests = "#{__dir__}/**/*_test.rb"
352
+ loader.ignore(tests)
353
+ loader.setup
354
+ ```
355
+
333
356
  ### Edge cases
334
357
 
335
358
  A class or module that acts as a namespace:
@@ -360,7 +383,7 @@ This only affects explicit namespaces, those idioms work well for any other ordi
360
383
 
361
384
  ## Pronunciation
362
385
 
363
- "Zeitwerk" is pronounced [this way](https://raw.githubusercontent.com/fxn/zeitwerk/master/extras/zeitwerk_pronunciation.mp3).
386
+ "Zeitwerk" is pronounced [this way](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3).
364
387
 
365
388
  ## Supported Ruby versions
366
389
 
@@ -376,7 +399,9 @@ On the other hand, autoloading in Rails is based on `const_missing`, which lacks
376
399
 
377
400
  I'd like to thank [@matthewd](https://github.com/matthewd) for the discussions we've had about this topic in the past years, I learned a couple of tricks used in Zeitwerk from him.
378
401
 
379
- Also would like to thank [@Shopify](https://github.com/Shopify), [@rafaelfranca](https://github.com/rafaelfranca), and [@dylanahsmith](https://github.com/dylanahsmith), for sharing [this PoC](https://github.com/Shopify/autoload_reloader). The technique Zeitwerk uses to support explicit namespaces was copied from that project.
402
+ Also, would like to thank [@Shopify](https://github.com/Shopify), [@rafaelfranca](https://github.com/rafaelfranca), and [@dylanahsmith](https://github.com/dylanahsmith), for sharing [this PoC](https://github.com/Shopify/autoload_reloader). The technique Zeitwerk uses to support explicit namespaces was copied from that project.
403
+
404
+ Finally, many thanks to [@schurig](https://github.com/schurig) for recording an [audio file](http://share.hashref.com/zeitwerk/zeitwerk_pronunciation.mp3) with the pronunciation of "Zeitwerk" in perfect German. 💯
380
405
 
381
406
  ## License
382
407
 
@@ -10,19 +10,20 @@ module Zeitwerk
10
10
  # @return [#call, nil]
11
11
  attr_accessor :logger
12
12
 
13
- # Absolute paths of directories from which you want to load constants. This
14
- # is a private attribute, client code should use `push_dir`.
15
- #
16
- # Stored in a hash to preserve order, easily handle duplicates, and also be
17
- # able to have a fast lookup, needed for detecting nested paths.
13
+ # Absolute paths of the root directories. Stored in a hash to preserve
14
+ # order, easily handle duplicates, and also be able to have a fast lookup,
15
+ # needed for detecting nested paths.
18
16
  #
19
17
  # "/Users/fxn/blog/app/assets" => true,
20
18
  # "/Users/fxn/blog/app/channels" => true,
21
19
  # ...
22
20
  #
21
+ # This is a private collection maintained by the loader. The public
22
+ # interface for it is `push_dir` and `dirs`.
23
+ #
23
24
  # @private
24
25
  # @return [{String => true}]
25
- attr_reader :dirs
26
+ attr_reader :root_dirs
26
27
 
27
28
  # Absolute paths of files or directories that have to be preloaded.
28
29
  #
@@ -30,12 +31,21 @@ module Zeitwerk
30
31
  # @return [<String>]
31
32
  attr_reader :preloads
32
33
 
33
- # Absolute paths of files or directories to be totally ignored.
34
+ # Absolute paths of files, directories, of glob patterns to be totally
35
+ # ignored.
34
36
  #
35
37
  # @private
36
38
  # @return [Set<String>]
37
39
  attr_reader :ignored
38
40
 
41
+ # The actual collection of absolute file and directory names at the time the
42
+ # ignored glob patterns were expanded. Computed on setup, and recomputed on
43
+ # reload.
44
+ #
45
+ # @private
46
+ # @return [Set<String>]
47
+ attr_reader :ignored_paths
48
+
39
49
  # Maps real absolute paths for which an autoload has been set to their
40
50
  # corresponding parent class or module and constant name.
41
51
  #
@@ -47,6 +57,12 @@ module Zeitwerk
47
57
  # @return [{String => (Module, String)}]
48
58
  attr_reader :autoloads
49
59
 
60
+ # Constant paths loaded so far.
61
+ #
62
+ # @private
63
+ # @return [Set<String>]
64
+ attr_reader :loaded
65
+
50
66
  # Maps constant paths of namespaces to arrays of corresponding directories.
51
67
  #
52
68
  # For example, given this mapping:
@@ -80,11 +96,13 @@ module Zeitwerk
80
96
  def initialize
81
97
  self.inflector = Inflector.new
82
98
 
83
- @dirs = {}
84
- @preloads = []
85
- @ignored = Set.new
86
- @autoloads = {}
87
- @lazy_subdirs = {}
99
+ @root_dirs = {}
100
+ @preloads = []
101
+ @ignored = Set.new
102
+ @ignored_paths = Set.new
103
+ @autoloads = {}
104
+ @loaded = Set.new
105
+ @lazy_subdirs = {}
88
106
 
89
107
  @mutex = Mutex.new
90
108
  @setup = false
@@ -101,6 +119,14 @@ module Zeitwerk
101
119
  Registry.register_loader(self)
102
120
  end
103
121
 
122
+ # Absolute paths of the root directories. This is a read-only collection,
123
+ # please push here via `push_dir`.
124
+ #
125
+ # @return [<String>]
126
+ def dirs
127
+ root_dirs.keys.freeze
128
+ end
129
+
104
130
  # Pushes `paths` to the list of root directories.
105
131
  #
106
132
  # @param path [<String, Pathname>]
@@ -109,7 +135,7 @@ module Zeitwerk
109
135
  abspath = File.expand_path(path)
110
136
  mutex.synchronize do
111
137
  if dir?(abspath)
112
- dirs[abspath] = true
138
+ root_dirs[abspath] = true
113
139
  else
114
140
  raise ArgumentError, "the root directory #{abspath} does not exist"
115
141
  end
@@ -135,7 +161,7 @@ module Zeitwerk
135
161
  end
136
162
  end
137
163
 
138
- # Files or directories to be totally ignored.
164
+ # Configure files, directories, or glob patterns to be totally ignored.
139
165
  #
140
166
  # @param paths [<String, Pathname, <String, Pathname>>]
141
167
  # @return [void]
@@ -143,13 +169,22 @@ module Zeitwerk
143
169
  mutex.synchronize { ignored.merge(expand_paths(paths)) }
144
170
  end
145
171
 
172
+ # @private
173
+ # @return [void]
174
+ def expand_ignored_glob_patterns
175
+ # Note that Dir.glob works with regular file names just fine. That is,
176
+ # glob patterns technically need no wildcards.
177
+ ignored_paths.replace(ignored.flat_map { |path| Dir.glob(path) })
178
+ end
179
+
146
180
  # Sets autoloads in the root namespace and preloads files, if any.
147
181
  #
148
182
  # @return [void]
149
183
  def setup
150
184
  mutex.synchronize do
151
185
  unless @setup
152
- actual_dirs.each { |dir| set_autoloads_in_dir(dir, Object) }
186
+ expand_ignored_glob_patterns
187
+ non_ignored_root_dirs.each { |dir| set_autoloads_in_dir(dir, Object) }
153
188
  do_preload
154
189
  @setup = true
155
190
  end
@@ -171,7 +206,7 @@ module Zeitwerk
171
206
  if parent.autoload?(cname)
172
207
  parent.send(:remove_const, cname)
173
208
  log("autoload for #{cpath(parent, cname)} removed") if logger
174
- elsif parent.const_defined?(cname, false)
209
+ elsif cdef?(parent, cname)
175
210
  parent.send(:remove_const, cname)
176
211
  log("#{cpath(parent, cname)} unloaded") if logger
177
212
  end
@@ -182,6 +217,7 @@ module Zeitwerk
182
217
  $LOADED_FEATURES.delete(path) if ruby?(path)
183
218
  end
184
219
  autoloads.clear
220
+ loaded.clear
185
221
  lazy_subdirs.clear
186
222
 
187
223
  Registry.on_unload(self)
@@ -211,13 +247,21 @@ module Zeitwerk
211
247
  def eager_load
212
248
  mutex.synchronize do
213
249
  unless @eager_loaded
214
- actual_dirs.each { |dir| eager_load_dir(dir) }
250
+ non_ignored_root_dirs.each { |dir| eager_load_dir(dir) }
215
251
  disable_tracer
216
252
  @eager_loaded = true
217
253
  end
218
254
  end
219
255
  end
220
256
 
257
+ # Says if the given constant path as been loaded.
258
+ #
259
+ # @param cpath [String]
260
+ # @return [Boolean]
261
+ def loaded?(cpath)
262
+ loaded.member?(cpath)
263
+ end
264
+
221
265
  # --- Class methods ---------------------------------------------------------------------------
222
266
 
223
267
  # This is a shortcut for
@@ -230,9 +274,6 @@ module Zeitwerk
230
274
  # except that this method returns the same object in subsequent calls from
231
275
  # the same file, in the unlikely case the gem wants to be able to reload.
232
276
  #
233
- # `Zeitwerk::GemInflector` is a subclass of `Zeitwerk::Inflector` that
234
- # camelizes "lib/my_gem/version.rb" as "MyGem::VERSION".
235
- #
236
277
  # @return [Zeitwerk::Loader]
237
278
  def self.for_gem
238
279
  called_from = caller[0].split(':')[0]
@@ -246,6 +287,14 @@ module Zeitwerk
246
287
  Registry.loaders.each(&:eager_load)
247
288
  end
248
289
 
290
+ # Returns an array with the absolute paths of the root directories of all
291
+ # registered loaders. This is a read-only collection.
292
+ #
293
+ # @return [<String>]
294
+ def self.all_dirs
295
+ Registry.loaders.flat_map(&:dirs).freeze
296
+ end
297
+
249
298
  # --- Callbacks -------------------------------------------------------------------------------
250
299
 
251
300
  # Callback invoked from Kernel when a managed file is loaded.
@@ -254,10 +303,9 @@ module Zeitwerk
254
303
  # @param file [String]
255
304
  # @return [void]
256
305
  def on_file_loaded(file)
257
- if logger
258
- parent, cname = autoloads[file]
259
- log("constant #{cpath(parent, cname)} loaded from file #{file}")
260
- end
306
+ parent, cname = autoloads[file]
307
+ loaded.add(cpath(parent, cname))
308
+ log("constant #{cpath(parent, cname)} loaded from file #{file}") if logger
261
309
  end
262
310
 
263
311
  # Callback invoked from Kernel when a managed directory is loaded.
@@ -267,6 +315,7 @@ module Zeitwerk
267
315
  # @return [void]
268
316
  def on_dir_loaded(dir)
269
317
  parent, cname = autoloads[dir]
318
+ loaded.add(cpath(parent, cname))
270
319
  autovivified = parent.const_set(cname, Module.new)
271
320
  log("module #{cpath(parent, cname)} autovivified from directory #{dir}") if logger
272
321
 
@@ -278,8 +327,8 @@ module Zeitwerk
278
327
  private # -------------------------------------------------------------------------------------
279
328
 
280
329
  # @return [<String>]
281
- def actual_dirs
282
- dirs.keys.delete_if { |dir| ignored.member?(dir) }
330
+ def non_ignored_root_dirs
331
+ root_dirs.keys.delete_if { |dir| ignored_paths.member?(dir) }
283
332
  end
284
333
 
285
334
  # @param dir [String]
@@ -297,7 +346,7 @@ module Zeitwerk
297
346
  # To resolve the ambiguity file name -> constant path this introduces,
298
347
  # the `app/models/concerns` directory is totally ignored as a namespace,
299
348
  # it counts only as root. The guard checks that.
300
- autoload_subdir(parent, cname, abspath) unless dirs.key?(abspath)
349
+ autoload_subdir(parent, cname, abspath) unless root_dirs.key?(abspath)
301
350
  end
302
351
  end
303
352
  end
@@ -307,13 +356,13 @@ module Zeitwerk
307
356
  # @param subdir [String]
308
357
  # @return [void]
309
358
  def autoload_subdir(parent, cname, subdir)
310
- if autoload = autoload_for?(parent, cname)
311
- enable_tracer if ruby?(autoload)
359
+ if autoload_path = autoload_for?(parent, cname)
360
+ enable_tracer if ruby?(autoload_path)
312
361
  # We do not need to issue another autoload, the existing one is enough
313
362
  # no matter if it is for a file or a directory. Just remember the
314
363
  # subdirectory has to be visited if the namespace is used.
315
364
  (lazy_subdirs[cpath(parent, cname)] ||= []) << subdir
316
- elsif !parent.const_defined?(cname, false)
365
+ elsif !cdef?(parent, cname)
317
366
  # First time we find this namespace, set an autoload for it.
318
367
  (lazy_subdirs[cpath(parent, cname)] ||= []) << subdir
319
368
  set_autoload(parent, cname, subdir)
@@ -340,7 +389,7 @@ module Zeitwerk
340
389
 
341
390
  set_autoload(parent, cname, file)
342
391
  enable_tracer
343
- elsif !parent.const_defined?(cname, false)
392
+ elsif !cdef?(parent, cname)
344
393
  set_autoload(parent, cname, file)
345
394
  end
346
395
  end
@@ -382,11 +431,14 @@ module Zeitwerk
382
431
  # @param dir [String]
383
432
  # @return [void]
384
433
  def eager_load_dir(dir)
385
- each_abspath(dir) do |abspath|
386
- if ruby?(abspath)
387
- require abspath
388
- elsif dir?(abspath)
389
- eager_load_dir(abspath)
434
+ queue = [dir]
435
+ while dir = queue.shift
436
+ each_abspath(dir) do |abspath|
437
+ if ruby?(abspath)
438
+ require abspath
439
+ elsif dir?(abspath)
440
+ queue << abspath
441
+ end
390
442
  end
391
443
  end
392
444
  end
@@ -438,7 +490,7 @@ module Zeitwerk
438
490
  Dir.foreach(dir) do |entry|
439
491
  next if entry.start_with?(".")
440
492
  abspath = File.join(dir, entry)
441
- yield abspath unless ignored.member?(abspath)
493
+ yield abspath unless ignored_paths.member?(abspath)
442
494
  end
443
495
  end
444
496
 
@@ -475,5 +527,9 @@ module Zeitwerk
475
527
  def disable_tracer
476
528
  tracer.disable if tracer.enabled?
477
529
  end
530
+
531
+ def cdef?(parent, cname)
532
+ parent.const_defined?(cname, false)
533
+ end
478
534
  end
479
535
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "1.0.0.beta2"
4
+ VERSION = "1.0.0.beta3"
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.0.0.beta2
4
+ version: 1.0.0.beta3
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-01-22 00:00:00.000000000 Z
11
+ date: 2019-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem