Module: Kernel

Defined in:
lib/im/kernel.rb

Instance Method Summary collapse

Instance Method Details

#im_original_requireObject



4
# File 'lib/im/kernel.rb', line 4

alias_method :im_original_require, :require

#require(path) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/im/kernel.rb', line 6

def require(path)
  if Im.autoloads.key?(path)
    return Im.with_import(Im.autoloads.delete(path)) do |_context|
      !!Im.import(path)
    end
  end

  if Im.importing? && (resolved = $LOAD_PATH.resolve_feature_path(path))
    resolved_path = resolved[1]
    Im.registry[resolved_path] ||= Im::Dependency.new(resolved_path, Im.current_import)
  end

  loaded = im_original_require(path)
  return loaded unless Im.importing?

  if resolved
    dependency = Im.registry[resolved_path]

    if loaded && Im.registry.key?(location = caller_locations(1, 1).first.path)
      Im.registry[location].dependencies << dependency
    end

    dependency.dependencies.each { |d| require d.path }

    dependency.modules.each do |m|
      name = m.name

      # Do not assign constants that are aliased to root namespace
      root = name.split("::", 2)[0]
      next if Object.const_defined?(root) &&
        Im.current_import.const_defined?(root, false) &&
        Object.const_get(root) == Im.current_import.const_get(root)

      Im.current_import.const_set(name, m) unless Im.current_import.const_defined?(name, false)
    end
  end

  return loaded
end