Module: Imports

Defined in:
lib/import.rb

Defined Under Namespace

Modules: DSL Classes: Context, Export

Class Method Summary collapse

Class Method Details

.registerObject



2
3
4
# File 'lib/import.rb', line 2

def self.register
  @register ||= Hash.new
end

.resolve_path(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
# File 'lib/import.rb', line 6

def self.resolve_path(path)
  # import('./test') and import('../test') behave like require_relative.
  if path.start_with?('.')
    caller_file = caller_locations.first.absolute_path

    unless caller_file
      raise "Error when importing #{path}: caller[0] is #{caller[0]}"
    end

    base_dir = caller_file.split('/')[0..-2].join('/')
    path = File.expand_path("#{base_dir}/#{path}")
  end

  if File.file?(path)
    fullpath = path
  elsif File.file?("#{path}.rb")
    fullpath = "#{path}.rb"
  else
    $:.each do |directory|
      choices  = [File.join(directory, path), File.join(directory, "#{path}.rb")]
      fullpath = choices.find { |choice| File.file?(choice) }
    end
    if ! defined?(fullpath) || fullpath.nil?
      raise LoadError, "no such file to import -- #{path}"
    end
  end

  File.expand_path(fullpath)
end