Module: Loader

Defined in:
lib/modules/loader.rb

Defined Under Namespace

Modules: Api

Constant Summary collapse

DEBUG =
Debug.debug(File.basename(__FILE__))

Class Method Summary collapse

Class Method Details

.export(value) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/modules/loader.rb', line 20

def self.export(value)
  callsite = Callsite.resolve
  if @cache.include?(callsite) && value.class == Hash
    # Special handling to enable multiple exports
    @cache[callsite] = @cache[callsite].merge(value)
  else
    @cache[callsite] = value
  end
end

.import(id, type = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/modules/loader.rb', line 30

def self.import(id, type=nil)
  if type == 'interop'
    return Interop.import(id, save_the_environment: @save_the_environment)
  end

  callsite = Callsite.resolve
  parent = @import_called ? File.dirname(callsite) : @basepath
  raw = File.join(parent, id)
  path = File.expand_path(raw)
  filepath = path.end_with?('.rb') ? path : "#{path}.rb"
  exists = File.exist?(filepath)

  if type == 'internal' && !exists
    raise "Could not resolve local module at #{path}"
  end

  if exists
    # Prefer loading local module since we found it.
    begin
      Kernel.load(filepath, true) unless @cache.include?(filepath)
    rescue => e
      raise LoadError, "Could not load #{filepath} from #{parent}: #{e}"
    end

    return @cache[filepath]
  end

  # Failover to external load.
  return Interop.import(id, save_the_environment: @save_the_environment)
end