Module: Hecks::Namespace

Defined in:
lib/hecks/facade.rb

Overview

The one careful way to put something at top level.

Overwrites only what it itself installed (tracked in GENERATED), refuses — with a warning, never a raise — to clobber a constant belonging to user code or the stdlib. Which is why a chapter named Set never becomes a constant, and why Registry keeps its own table of chapters rather than trusting Ruby's. Each re-install REPLACES the previous boot's entry, so what GENERATED retains is one small facade module per name, not a graph per boot.

Constant Summary collapse

GENERATED =
{}

Class Method Summary collapse

Class Method Details

.install(container, name, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hecks/facade.rb', line 22

def install(container, name, value)
  name = name.to_s

  if container.const_defined?(name, false)
    current = container.const_get(name)
    unless GENERATED[[container, name]].equal?(current)
      warn "[hecks] #{name} is already defined — leaving it alone"
      return current
    end
    container.send(:remove_const, name)
  end

  container.const_set(name, value)
  GENERATED[[container, name]] = value
  value
end