Class: Zeitwerk::Loader
- Inherits:
-
Object
- Object
- Zeitwerk::Loader
- Includes:
- Callbacks, RealModName
- Defined in:
- lib/zeitwerk/loader.rb
Defined Under Namespace
Modules: Callbacks
Class Attribute Summary collapse
-
.default_logger ⇒ Object
Returns the value of attribute default_logger.
- .mutex ⇒ Object
Instance Attribute Summary collapse
-
#autoloaded_dirs ⇒ Object
readonly
We keep track of autoloaded directories to remove them from the registry at the end of eager loading.
-
#autoloads ⇒ Object
readonly
Maps real absolute paths for which an autoload has been set —and not executed— to their corresponding parent class or module and constant name.
-
#collapse_dirs ⇒ Object
readonly
The actual collection of absolute directory names at the time the collapse glob patterns were expanded.
-
#collapse_glob_patterns ⇒ Object
readonly
Absolute paths of directories or glob patterns to be collapsed.
-
#eager_load_exclusions ⇒ Object
readonly
Absolute paths of files or directories not to be eager loaded.
-
#ignored_glob_patterns ⇒ Object
readonly
Absolute paths of files, directories, or glob patterns to be totally ignored.
-
#ignored_paths ⇒ Object
readonly
The actual collection of absolute file and directory names at the time the ignored glob patterns were expanded.
-
#inflector ⇒ Object
Returns the value of attribute inflector.
-
#lazy_subdirs ⇒ Object
readonly
Maps constant paths of namespaces to arrays of corresponding directories.
-
#logger ⇒ Object
Returns the value of attribute logger.
- #mutex ⇒ Object readonly
- #mutex2 ⇒ Object readonly
-
#on_load_callbacks ⇒ Object
readonly
User-oriented callbacks to be fired when a constant is loaded.
-
#preloads ⇒ Object
readonly
Absolute paths of files or directories that have to be preloaded.
-
#root_dirs ⇒ Object
readonly
Absolute paths of the root directories.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#to_unload ⇒ Object
readonly
Stores metadata needed for unloading.
Class Method Summary collapse
-
.all_dirs ⇒ Object
Returns an array with the absolute paths of the root directories of all registered loaders.
-
.eager_load_all ⇒ Object
Broadcasts ‘eager_load` to all loaders.
-
.for_gem ⇒ Object
This is a shortcut for.
Instance Method Summary collapse
-
#collapse(*glob_patterns) ⇒ Object
Configure directories or glob patterns to be collapsed.
-
#dirs ⇒ Object
Absolute paths of the root directories.
-
#do_not_eager_load(*paths) ⇒ Object
Let eager load ignore the given files or directories.
-
#eager_load ⇒ Object
Eager loads all files in the root directories, recursively.
-
#enable_reloading ⇒ Object
You need to call this method before setup in order to be able to reload.
-
#ignore(*glob_patterns) ⇒ Object
Configure files, directories, or glob patterns to be totally ignored.
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
-
#log! ⇒ Object
Logs to ‘$stdout`, handy shortcut for debugging.
- #manages?(dir) ⇒ Boolean
-
#on_load(cpath, &block) ⇒ Object
Configure a block to be invoked once a certain constant path is loaded.
-
#preload(*paths) ⇒ Object
Files or directories to be preloaded instead of lazy loaded.
-
#push_dir(path, namespace: Object) ⇒ Object
Pushes ‘path` to the list of root directories.
-
#reload ⇒ Object
Unloads all loaded code, and calls setup again so that the loader is able to pick any changes in the file system.
- #reloading_enabled? ⇒ Boolean
-
#setup ⇒ Object
Sets autoloads in the root namespace and preloads files, if any.
-
#unload ⇒ Object
Removes loaded constants and configured autoloads.
-
#unloadable_cpath?(cpath) ⇒ Boolean
Says if the given constant path would be unloaded on reload.
-
#unloadable_cpaths ⇒ Object
Returns an array with the constant paths that would be unloaded on reload.
Methods included from RealModName
Methods included from Callbacks
#on_dir_autoloaded, #on_file_autoloaded, #on_namespace_loaded
Constructor Details
#initialize ⇒ Loader
Returns a new instance of Loader.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/zeitwerk/loader.rb', line 143 def initialize @initialized_at = Time.now @tag = SecureRandom.hex(3) @inflector = Inflector.new @logger = self.class.default_logger @root_dirs = {} @preloads = [] @ignored_glob_patterns = Set.new @ignored_paths = Set.new @collapse_glob_patterns = Set.new @collapse_dirs = Set.new @autoloads = {} @autoloaded_dirs = [] @to_unload = {} @lazy_subdirs = {} @eager_load_exclusions = Set.new @on_load_callbacks = {} # TODO: find a better name for these mutexes. @mutex = Mutex.new @mutex2 = Mutex.new @setup = false @eager_loaded = false @reloading_enabled = false Registry.register_loader(self) end |
Class Attribute Details
.default_logger ⇒ Object
Returns the value of attribute default_logger.
481 482 483 |
# File 'lib/zeitwerk/loader.rb', line 481 def default_logger @default_logger end |
.mutex ⇒ Object
485 486 487 |
# File 'lib/zeitwerk/loader.rb', line 485 def mutex @mutex end |
Instance Attribute Details
#autoloaded_dirs ⇒ Object (readonly)
We keep track of autoloaded directories to remove them from the registry at the end of eager loading.
Files are removed as they are autoloaded, but directories need to wait due to concurrency (see why in Zeitwerk::Loader::Callbacks#on_dir_autoloaded).
90 91 92 |
# File 'lib/zeitwerk/loader.rb', line 90 def autoloaded_dirs @autoloaded_dirs end |
#autoloads ⇒ Object (readonly)
Maps real absolute paths for which an autoload has been set —and not executed— to their corresponding parent class or module and constant name.
"/Users/fxn/blog/app/models/user.rb" => [Object, :User],
"/Users/fxn/blog/app/models/hotel/pricing.rb" => [Hotel, :Pricing]
...
80 81 82 |
# File 'lib/zeitwerk/loader.rb', line 80 def autoloads @autoloads end |
#collapse_dirs ⇒ Object (readonly)
The actual collection of absolute directory names at the time the collapse glob patterns were expanded. Computed on setup, and recomputed on reload.
68 69 70 |
# File 'lib/zeitwerk/loader.rb', line 68 def collapse_dirs @collapse_dirs end |
#collapse_glob_patterns ⇒ Object (readonly)
Absolute paths of directories or glob patterns to be collapsed.
61 62 63 |
# File 'lib/zeitwerk/loader.rb', line 61 def collapse_glob_patterns @collapse_glob_patterns end |
#eager_load_exclusions ⇒ Object (readonly)
Absolute paths of files or directories not to be eager loaded.
130 131 132 |
# File 'lib/zeitwerk/loader.rb', line 130 def eager_load_exclusions @eager_load_exclusions end |
#ignored_glob_patterns ⇒ Object (readonly)
Absolute paths of files, directories, or glob patterns to be totally ignored.
47 48 49 |
# File 'lib/zeitwerk/loader.rb', line 47 def ignored_glob_patterns @ignored_glob_patterns end |
#ignored_paths ⇒ Object (readonly)
The actual collection of absolute file and directory names at the time the ignored glob patterns were expanded. Computed on setup, and recomputed on reload.
55 56 57 |
# File 'lib/zeitwerk/loader.rb', line 55 def ignored_paths @ignored_paths end |
#inflector ⇒ Object
Returns the value of attribute inflector.
16 17 18 |
# File 'lib/zeitwerk/loader.rb', line 16 def inflector @inflector end |
#lazy_subdirs ⇒ Object (readonly)
Maps constant paths of namespaces to arrays of corresponding directories.
For example, given this mapping:
"Admin" => [
"/Users/fxn/blog/app/controllers/admin",
"/Users/fxn/blog/app/models/admin",
...
]
when ‘Admin` gets defined we know that it plays the role of a namespace and that its children are spread over those directories. We’ll visit them to set up the corresponding autoloads.
124 125 126 |
# File 'lib/zeitwerk/loader.rb', line 124 def lazy_subdirs @lazy_subdirs end |
#logger ⇒ Object
Returns the value of attribute logger.
19 20 21 |
# File 'lib/zeitwerk/loader.rb', line 19 def logger @logger end |
#mutex ⇒ Object (readonly)
137 138 139 |
# File 'lib/zeitwerk/loader.rb', line 137 def mutex @mutex end |
#mutex2 ⇒ Object (readonly)
141 142 143 |
# File 'lib/zeitwerk/loader.rb', line 141 def mutex2 @mutex2 end |
#on_load_callbacks ⇒ Object (readonly)
User-oriented callbacks to be fired when a constant is loaded.
133 134 135 |
# File 'lib/zeitwerk/loader.rb', line 133 def on_load_callbacks @on_load_callbacks end |
#preloads ⇒ Object (readonly)
Absolute paths of files or directories that have to be preloaded.
40 41 42 |
# File 'lib/zeitwerk/loader.rb', line 40 def preloads @preloads end |
#root_dirs ⇒ Object (readonly)
Absolute paths of the root directories. Stored in a hash to preserve order, easily handle duplicates, and also be able to have a fast lookup, needed for detecting nested paths.
"/Users/fxn/blog/app/assets" => true,
"/Users/fxn/blog/app/channels" => true,
...
This is a private collection maintained by the loader. The public interface for it is ‘push_dir` and `dirs`.
34 35 36 |
# File 'lib/zeitwerk/loader.rb', line 34 def root_dirs @root_dirs end |
#tag ⇒ Object
Returns the value of attribute tag.
13 14 15 |
# File 'lib/zeitwerk/loader.rb', line 13 def tag @tag end |
#to_unload ⇒ Object (readonly)
Stores metadata needed for unloading. Its entries look like this:
"Admin::Role" => [".../admin/role.rb", [Admin, :Role]]
The cpath as key helps implementing unloadable_cpath? The real file name is stored in order to be able to delete it from $LOADED_FEATURES, and the pair [Module, Symbol] is used to remove_const the constant from the class or module object.
If reloading is enabled, this hash is filled as constants are autoloaded or eager loaded. Otherwise, the collection remains empty.
106 107 108 |
# File 'lib/zeitwerk/loader.rb', line 106 def to_unload @to_unload end |
Class Method Details
.all_dirs ⇒ Object
Returns an array with the absolute paths of the root directories of all registered loaders. This is a read-only collection.
515 516 517 |
# File 'lib/zeitwerk/loader.rb', line 515 def all_dirs Registry.loaders.flat_map(&:dirs).freeze end |
.eager_load_all ⇒ Object
Broadcasts ‘eager_load` to all loaders.
507 508 509 |
# File 'lib/zeitwerk/loader.rb', line 507 def eager_load_all Registry.loaders.each(&:eager_load) end |
.for_gem ⇒ Object
This is a shortcut for
require "zeitwerk"
loader = Zeitwerk::Loader.new
loader.tag = File.basename(__FILE__, ".rb")
loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
loader.push_dir(__dir__)
except that this method returns the same object in subsequent calls from the same file, in the unlikely case the gem wants to be able to reload.
499 500 501 502 |
# File 'lib/zeitwerk/loader.rb', line 499 def for_gem called_from = caller_locations(1, 1).first.path Registry.loader_for_gem(called_from) end |
Instance Method Details
#collapse(*glob_patterns) ⇒ Object
Configure directories or glob patterns to be collapsed.
261 262 263 264 265 266 267 |
# File 'lib/zeitwerk/loader.rb', line 261 def collapse(*glob_patterns) glob_patterns = (glob_patterns) mutex.synchronize do collapse_glob_patterns.merge(glob_patterns) collapse_dirs.merge((glob_patterns)) end end |
#dirs ⇒ Object
Absolute paths of the root directories. This is a read-only collection, please push here via ‘push_dir`.
186 187 188 |
# File 'lib/zeitwerk/loader.rb', line 186 def dirs root_dirs.keys.freeze end |
#do_not_eager_load(*paths) ⇒ Object
Let eager load ignore the given files or directories. The constants defined in those files are still autoloadable.
435 436 437 |
# File 'lib/zeitwerk/loader.rb', line 435 def do_not_eager_load(*paths) mutex.synchronize { eager_load_exclusions.merge((paths)) } end |
#eager_load ⇒ Object
Eager loads all files in the root directories, recursively. Files do not need to be in ‘$LOAD_PATH`, absolute file names are used. Ignored files are not eager loaded. You can opt-out specifically in specific files and directories with `do_not_eager_load`.
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/zeitwerk/loader.rb', line 392 def eager_load mutex.synchronize do break if @eager_loaded queue = [] actual_root_dirs.each do |root_dir, namespace| queue << [namespace, root_dir] unless eager_load_exclusions.member?(root_dir) end while to_eager_load = queue.shift namespace, dir = to_eager_load ls(dir) do |basename, abspath| next if eager_load_exclusions.member?(abspath) if ruby?(abspath) if cref = autoloads[File.realpath(abspath)] cref[0].const_get(cref[1], false) end elsif dir?(abspath) && !root_dirs.key?(abspath) if collapse_dirs.member?(abspath) queue << [namespace, abspath] else cname = inflector.camelize(basename, abspath) queue << [namespace.const_get(cname, false), abspath] end end end end autoloaded_dirs.each do |autoloaded_dir| Registry.unregister_autoload(autoloaded_dir) end autoloaded_dirs.clear @eager_loaded = true end end |
#enable_reloading ⇒ Object
You need to call this method before setup in order to be able to reload. There is no way to undo this, either you want to reload or you don’t.
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/zeitwerk/loader.rb', line 218 def enable_reloading mutex.synchronize do break if @reloading_enabled if @setup raise Error, "cannot enable reloading after setup" else @reloading_enabled = true end end end |
#ignore(*glob_patterns) ⇒ Object
Configure files, directories, or glob patterns to be totally ignored.
250 251 252 253 254 255 256 |
# File 'lib/zeitwerk/loader.rb', line 250 def ignore(*glob_patterns) glob_patterns = (glob_patterns) mutex.synchronize do ignored_glob_patterns.merge(glob_patterns) ignored_paths.merge((glob_patterns)) end end |
#log! ⇒ Object
Logs to ‘$stdout`, handy shortcut for debugging.
458 459 460 |
# File 'lib/zeitwerk/loader.rb', line 458 def log! @logger = ->(msg) { puts msg } end |
#manages?(dir) ⇒ Boolean
464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/zeitwerk/loader.rb', line 464 def manages?(dir) dir = dir + "/" ignored_paths.each do |ignored_path| return false if dir.start_with?(ignored_path + "/") end root_dirs.each_key do |root_dir| return true if root_dir.start_with?(dir) || dir.start_with?(root_dir + "/") end false end |
#on_load(cpath, &block) ⇒ Object
Configure a block to be invoked once a certain constant path is loaded. Supports multiple callbacks, and if there are many, they are executed in the order in which they were defined.
loader.on_load("SomeApiClient") do
SomeApiClient.endpoint = "https://api.dev"
end
279 280 281 282 283 284 285 |
# File 'lib/zeitwerk/loader.rb', line 279 def on_load(cpath, &block) raise TypeError, "on_load only accepts strings" unless cpath.is_a?(String) mutex.synchronize do (on_load_callbacks[cpath] ||= []) << block end end |
#preload(*paths) ⇒ Object
Files or directories to be preloaded instead of lazy loaded.
238 239 240 241 242 243 244 245 |
# File 'lib/zeitwerk/loader.rb', line 238 def preload(*paths) mutex.synchronize do (paths).each do |abspath| preloads << abspath do_preload_abspath(abspath) if @setup end end end |
#push_dir(path, namespace: Object) ⇒ Object
Pushes ‘path` to the list of root directories.
Raises ‘Zeitwerk::Error` if `path` does not exist, or if another loader in the same process already manages that directory or one of its ascendants or descendants.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/zeitwerk/loader.rb', line 198 def push_dir(path, namespace: Object) # Note that Class < Module. unless namespace.is_a?(Module) raise Error, "#{namespace.inspect} is not a class or module object, should be" end abspath = File.(path) if dir?(abspath) raise_if_conflicting_directory(abspath) root_dirs[abspath] = namespace else raise Error, "the root directory #{abspath} does not exist" end end |
#reload ⇒ Object
Unloads all loaded code, and calls setup again so that the loader is able to pick any changes in the file system.
This method is not thread-safe, please see how this can be achieved by client code in the README of the project.
375 376 377 378 379 380 381 382 383 384 |
# File 'lib/zeitwerk/loader.rb', line 375 def reload if reloading_enabled? unload recompute_ignored_paths recompute_collapse_dirs setup else raise ReloadingDisabledError, "can't reload, please call loader.enable_reloading before setup" end end |
#reloading_enabled? ⇒ Boolean
231 232 233 |
# File 'lib/zeitwerk/loader.rb', line 231 def reloading_enabled? @reloading_enabled end |
#setup ⇒ Object
Sets autoloads in the root namespace and preloads files, if any.
290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/zeitwerk/loader.rb', line 290 def setup mutex.synchronize do break if @setup actual_root_dirs.each do |root_dir, namespace| set_autoloads_in_dir(root_dir, namespace) end do_preload @setup = true end end |
#unload ⇒ Object
Removes loaded constants and configured autoloads.
The objects the constants stored are no longer reachable through them. In addition, since said objects are normally not referenced from anywhere else, they are eligible for garbage collection, which would effectively unload them.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/zeitwerk/loader.rb', line 312 def unload mutex.synchronize do # We are going to keep track of the files that were required by our # autoloads to later remove them from $LOADED_FEATURES, thus making them # loadable by Kernel#require again. # # Directories are not stored in $LOADED_FEATURES, keeping track of files # is enough. unloaded_files = Set.new autoloads.each do |realpath, (parent, cname)| if parent.autoload?(cname) unload_autoload(parent, cname) else # Could happen if loaded with require_relative. That is unsupported, # and the constant path would escape unloadable_cpath? This is just # defensive code to clean things up as much as we are able to. unload_cref(parent, cname) if cdef?(parent, cname) unloaded_files.add(realpath) if ruby?(realpath) end end to_unload.each_value do |(realpath, (parent, cname))| unload_cref(parent, cname) if cdef?(parent, cname) unloaded_files.add(realpath) if ruby?(realpath) end unless unloaded_files.empty? # Bootsnap decorates Kernel#require to speed it up using a cache and # this optimization does not check if $LOADED_FEATURES has the file. # # To make it aware of changes, the gem defines singleton methods in # $LOADED_FEATURES: # # https://github.com/Shopify/bootsnap/blob/master/lib/bootsnap/load_path_cache/core_ext/loaded_features.rb # # Rails applications may depend on bootsnap, so for unloading to work # in that setting it is preferable that we restrict our API choice to # one of those methods. $LOADED_FEATURES.reject! { |file| unloaded_files.member?(file) } end autoloads.clear autoloaded_dirs.clear to_unload.clear lazy_subdirs.clear Registry.on_unload(self) ExplicitNamespace.unregister(self) @setup = false @eager_loaded = false end end |
#unloadable_cpath?(cpath) ⇒ Boolean
Says if the given constant path would be unloaded on reload. This predicate returns ‘false` if reloading is disabled.
443 444 445 |
# File 'lib/zeitwerk/loader.rb', line 443 def unloadable_cpath?(cpath) to_unload.key?(cpath) end |
#unloadable_cpaths ⇒ Object
Returns an array with the constant paths that would be unloaded on reload. This predicate returns an empty array if reloading is disabled.
451 452 453 |
# File 'lib/zeitwerk/loader.rb', line 451 def unloadable_cpaths to_unload.keys.freeze end |