Class: Zeitwerk::Registry::ExplicitNamespaces
- Inherits:
-
Object
- Object
- Zeitwerk::Registry::ExplicitNamespaces
- Defined in:
- lib/zeitwerk/registry/explicit_namespaces.rb
Overview
A registry for explicit namespaces.
When a loader determines that a certain file should define an explicit namespace, it registers it here, associating its cref with itself.
If the namespace is autoloaded, our const_added callback retrieves its loader by calling loader_for. That way, the loader is able to scan the subdirectories that conform the namespace and set autoloads for their expected constants just in time.
Once autoloaded, the namespace is unregistered.
The implementation assumes an explicit namespace is managed by one loader. Loaders that reopen namespaces owned by other projects are responsible for loading their constant before setup. This is documented.
This is a private module.
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ ExplicitNamespaces
constructor
A new instance of ExplicitNamespaces.
- #loader_for(mod, cname) ⇒ Object
-
#register(cref, loader) ⇒ Object
Registers
crefas being the constant path of an explicit namespace managed byloader. -
#registered?(cref) ⇒ Boolean
This is an internal method only used by the test suite.
- #unregister_loader(loader) ⇒ Object
Constructor Details
#initialize ⇒ ExplicitNamespaces
Returns a new instance of ExplicitNamespaces.
23 24 25 26 27 28 29 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 23 def initialize # Maps crefs of explicit namespaces with their corresponding loader. # # Entries are added as the namespaces are found, and removed as they are # autoloaded. @loaders = Zeitwerk::Cref::Map.new end |
Instance Method Details
#clear ⇒ Object
57 58 59 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 57 def clear # for tests @loaders.clear end |
#loader_for(mod, cname) ⇒ Object
40 41 42 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 40 def loader_for(mod, cname) @loaders.delete_mod_cname(mod, cname) end |
#register(cref, loader) ⇒ Object
Registers cref as being the constant path of an explicit namespace
managed by loader.
35 36 37 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 35 def register(cref, loader) @loaders[cref] = loader end |
#registered?(cref) ⇒ Boolean
This is an internal method only used by the test suite.
52 53 54 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 52 def registered?(cref) @loaders[cref] end |
#unregister_loader(loader) ⇒ Object
45 46 47 |
# File 'lib/zeitwerk/registry/explicit_namespaces.rb', line 45 def unregister_loader(loader) @loaders.delete_by_value(loader) end |