Module: Im::RootNamespaceRedirect

Defined in:
lib/im.rb

Overview

This is a hack to catch references to top-level constants (::Foo) and, if the calling line is a key in the Im registry, replace the toplevel constant reference with its corresponding constant under the import namespace.

By doing this, a reference to ::Foo in an imported file will resolve to mod::Foo, where mod is the import module. Ideally this should ultimately happen at the Ruby implementation level.

Instance Method Summary collapse

Instance Method Details

#const_missing(name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/im.rb', line 90

def const_missing(name)

  # Take top five locations to allow for other `const_missing` overrides
  # in the backtrace. If there are more than five, then this will no longer
  # work. This is currently very inefficient.
  if location = caller_locations(1, 5).find { |l| Im.registry.key?(l.path) }
    Im.registry[location.path].import.const_get(name)
  else
    super
  end
end