zeitwerk 2.3.1 → 2.4.0

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: 7b0087cb3e996cc4e5ccfabf179c0e793a811d24e4ee36debf9e07fcb06e1630
4
- data.tar.gz: 63c94cc509932eb9dfceb9e951f05092c30e9d3aff8bacda5a66d7da93bf4a64
3
+ metadata.gz: 682fa352a9d6d4bf68d07cf2cbfbb539de3cfec4e8af2f930894ab27c707ca93
4
+ data.tar.gz: 1e7ce6157e6046cdb60f0f1b531f1f39fb97cad3537392114be5cf84dae672c9
5
5
  SHA512:
6
- metadata.gz: 6b978a994c59c4da2e50b32a825626a3e48c4e9e8f3455320f22799437c5fc81de4f3635ab8ea07626f42f03cbea71dc735c70b446942d8e914d5dd1104d15dc
7
- data.tar.gz: 2a0d8301b268d419be3dd36902ba44236e9de4a502c203bcbc2d32448f1c1d4e85fbae294b57d200a253e8e61e8ef07c119c9bf5f50e905d50064be6fe62babc
6
+ metadata.gz: e308baba1a8fbe4d7c64d7195c753d795a53caefa7264d2404117cb620f2081f6b0fc05c056dafb035b44430102e4472b7eb9fb3a4fd9d70aa124b7f9a3a8313
7
+ data.tar.gz: 5139d5abaeed86e256493b592460b13b19fd7602f7c099f7017b55af9f16a91429e84074a4e31fe79bf28235e02cbba3055cc4bda579bbc7de2a391c5997a393
data/README.md CHANGED
@@ -139,6 +139,22 @@ app/models/user.rb -> User
139
139
  app/controllers/admin/users_controller.rb -> Admin::UsersController
140
140
  ```
141
141
 
142
+ Alternatively, you can associate a custom namespace to a root directory by passing a class or module object in the optional `namespace` keyword argument.
143
+
144
+ For example, Active Job queue adapters have to define a constant after their name in `ActiveJob::QueueAdapters`.
145
+
146
+ So, if you declare
147
+
148
+ ```ruby
149
+ require "active_job"
150
+ require "active_job/queue_adapters"
151
+ loader.push_dir("#{__dir__}/adapters", namespace: ActiveJob::QueueAdapters)
152
+ ```
153
+
154
+ your adapter can be stored directly in that directory instead of the canonical `lib/active_job/queue_adapters`.
155
+
156
+ Please, note that the given namespace must be non-reloadable, though autoloaded constants in that namespace can be. That is, if you associate `app/api` with an existing `Api` module, that module should not be reloadable. However, if the project defines and autoloads the class `Api::V2::Deliveries`, that one can be reloaded.
157
+
142
158
  <a id="markdown-implicit-namespaces" name="implicit-namespaces"></a>
143
159
  ### Implicit namespaces
144
160
 
@@ -15,7 +15,7 @@ module Zeitwerk
15
15
  # @param _abspath [String]
16
16
  # @return [String]
17
17
  def camelize(basename, _abspath)
18
- overrides[basename] || basename.split('_').map!(&:capitalize).join
18
+ overrides[basename] || basename.split('_').each(&:capitalize!).join
19
19
  end
20
20
 
21
21
  # Configures hard-coded inflections:
@@ -190,13 +190,19 @@ module Zeitwerk
190
190
  # or descendants.
191
191
  #
192
192
  # @param path [<String, Pathname>]
193
+ # @param namespace [Class, Module]
193
194
  # @raise [Zeitwerk::Error]
194
195
  # @return [void]
195
- def push_dir(path)
196
+ def push_dir(path, namespace: Object)
197
+ # Note that Class < Module.
198
+ unless namespace.is_a?(Module)
199
+ raise Error, "#{namespace.inspect} is not a class or module object, should be"
200
+ end
201
+
196
202
  abspath = File.expand_path(path)
197
203
  if dir?(abspath)
198
204
  raise_if_conflicting_directory(abspath)
199
- root_dirs[abspath] = true
205
+ root_dirs[abspath] = namespace
200
206
  else
201
207
  raise Error, "the root directory #{abspath} does not exist"
202
208
  end
@@ -268,7 +274,9 @@ module Zeitwerk
268
274
  mutex.synchronize do
269
275
  break if @setup
270
276
 
271
- actual_root_dirs.each { |root_dir| set_autoloads_in_dir(root_dir, Object) }
277
+ actual_root_dirs.each do |root_dir, namespace|
278
+ set_autoloads_in_dir(root_dir, namespace)
279
+ end
272
280
  do_preload
273
281
 
274
282
  @setup = true
@@ -368,8 +376,11 @@ module Zeitwerk
368
376
  mutex.synchronize do
369
377
  break if @eager_loaded
370
378
 
371
- queue = actual_root_dirs.reject { |dir| eager_load_exclusions.member?(dir) }
372
- queue.map! { |dir| [Object, dir] }
379
+ queue = []
380
+ actual_root_dirs.each do |root_dir, namespace|
381
+ queue << [namespace, root_dir] unless eager_load_exclusions.member?(root_dir)
382
+ end
383
+
373
384
  while to_eager_load = queue.shift
374
385
  namespace, dir = to_eager_load
375
386
 
@@ -498,7 +509,7 @@ module Zeitwerk
498
509
 
499
510
  # @return [<String>]
500
511
  def actual_root_dirs
501
- root_dirs.keys.delete_if do |root_dir|
512
+ root_dirs.reject do |root_dir, _namespace|
502
513
  !dir?(root_dir) || ignored_paths.member?(root_dir)
503
514
  end
504
515
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zeitwerk
4
- VERSION = "2.3.1"
4
+ VERSION = "2.4.0"
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: 2.3.1
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Noria
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-28 00:00:00.000000000 Z
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Zeitwerk implements constant autoloading with Ruby semantics. Each gem