Module: Zeitwerk::ExplicitNamespace

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

Class Attribute Details

.cpaths{String => Zeitwerk::Loader} (readonly)

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

Returns:



16
17
18
# File 'lib/zeitwerk/explicit_namespace.rb', line 16

def cpaths
  @cpaths
end

.mutexMutex (readonly)

Returns:

  • (Mutex)


20
21
22
# File 'lib/zeitwerk/explicit_namespace.rb', line 20

def mutex
  @mutex
end

.tracerTracePoint (readonly)

Returns:

  • (TracePoint)


24
25
26
# File 'lib/zeitwerk/explicit_namespace.rb', line 24

def tracer
  @tracer
end

Class Method Details

.disable_tracer_if_unneededObject



50
51
52
53
54
# File 'lib/zeitwerk/explicit_namespace.rb', line 50

def disable_tracer_if_unneeded
  mutex.synchronize do
    tracer.disable if cpaths.empty?
  end
end

.register(cpath, loader) ⇒ void

This method returns an undefined value.

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

Parameters:



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) ⇒ void

This method returns an undefined value.

Parameters:



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

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