Module: Zeitwerk::ExplicitNamespace

Extended by:
RealModName
Defined in:
lib/zeitwerk/explicit_namespace.rb

Overview

Centralizes the logic for the trace point used to detect the creation of explicit namespaces, needed to descend into matching subdirectories right after the constant has been defined.

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.

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from RealModName

real_mod_name

Class Attribute Details

.cpathsObject (readonly)

Maps constant paths that correspond to explicit namespaces according to the file system, to the loader responsible for them.



18
19
20
# File 'lib/zeitwerk/explicit_namespace.rb', line 18

def cpaths
  @cpaths
end

.mutexObject (readonly)



22
23
24
# File 'lib/zeitwerk/explicit_namespace.rb', line 22

def mutex
  @mutex
end

.tracerObject (readonly)



26
27
28
# File 'lib/zeitwerk/explicit_namespace.rb', line 26

def tracer
  @tracer
end

Class Method Details

.register(cpath, loader) ⇒ Object

Asserts ‘cpath` corresponds to an explicit namespace for which `loader` is responsible.



33
34
35
36
37
38
39
40
# File 'lib/zeitwerk/explicit_namespace.rb', line 33

def register(cpath, loader)
  mutex.synchronize do
    cpaths[cpath] = loader
    # We check enabled? because, looking at the C source code, enabling an
    # enabled tracer does not seem to be a simple no-op.
    tracer.enable unless tracer.enabled?
  end
end

.unregister(loader) ⇒ Object



44
45
46
47
# File 'lib/zeitwerk/explicit_namespace.rb', line 44

def unregister(loader)
  cpaths.delete_if { |_cpath, l| l == loader }
  disable_tracer_if_unneeded
end