Module: RequireHere

Included in:
Module
Defined in:
lib/require_here.rb,
lib/require_here/version.rb

Defined Under Namespace

Modules: Version

Constant Summary collapse

LOOKUP =
{}

Instance Method Summary collapse

Instance Method Details

#require_here(file_path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/require_here.rb', line 6

def require_here(file_path)
  # Existing constants before require.
  existing = Object.constants

  require(file_path)

  # Introduced by require.
  introduced = Object.constants - existing

  # Move each introduced constant into the current namespace.
  introduced.each_with_index do |c, i|
    # Keep a reference to the constant.
    LOOKUP[i] = Object.const_get(c)
    # Remove the constant from the root.
    Object.send(:remove_const, c)
    # Place it in the current namespace.
    module_eval %Q(#{c} = ::RequireHere::LOOKUP[#{i}])
  end

  LOOKUP.clear
end