Module: Loader

Defined in:
lib/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



16
17
18
19
20
21
22
23
# File 'lib/loader.rb', line 16

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

.import(id, type = nil) ⇒ Object



25
26
27
28
29
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
60
61
62
# File 'lib/loader.rb', line 25

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

  prev = @path
  if @path.nil?
    container = @basepath
    raw = File.join(container, id)
  else
    container = File.dirname(@path)
    raw = File.join(container, id)
  end

  @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?(@path)
    rescue
      raise LoadError, "Could not load #{filepath} from #{container}"
    end

    result = @cache[@path]
  else
    # Failover to external load.
    result = Interop.import(id)
  end

  @path = prev
  result
end