Module: Import

Defined in:
lib/import.rb

Constant Summary collapse

VERSION =
self.import('./import/version')::VERSION

Class Method Summary collapse

Class Method Details

.evaluate(path) ⇒ Module

Parameters:

  • path (String)

Returns:

  • (Module)


48
49
50
51
52
53
54
55
# File 'lib/import.rb', line 48

def evaluate(path)
  script = File.read(path)

  res = Module.new
  res.module_eval(script, path)

  return res
end

.exist_file(path, fname = nil) ⇒ String|nil

If file exists, returns full path. unless, returns nil

Parameters:

  • path (String)

    Base path

  • fname (String) (defaults to: nil)

    file name

Returns:

  • (String|nil)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/import.rb', line 9

def exist_file(path, fname = nil)
  pn = Pathname.new(path)
  pn = pn.join(fname) if fname

  full_path = pn.to_s

  return full_path if FileTest.file?(full_path)

  rb ="#{full_path}.rb"
  return rb if FileTest.file?(rb)

  return nil
end

.find_file(feature, base) ⇒ String|nil

Parameters:

  • feature (String)

Returns:

  • (String|nil)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/import.rb', line 25

def find_file(feature, base)
  # absolute path
  if feature[0] == '/'
    full_path = exist_file(feature)
    return full_path if full_path
  end

  # relative path
  if feature.start_with?('./') || feature.start_with?('../')
    full_path = exist_file(File.expand_path("../#{feature}", base))
    return full_path if full_path
  end

  $LOAD_PATH.each do |path|
    full_path = exist_file(path, feature)
    return full_path if full_path
  end

  return nil
end

.import(feature, base = caller_locations[0].absolute_path) ⇒ Object

Parameters:

  • feature (String)
  • base (String) (defaults to: caller_locations[0].absolute_path)

Raises:

  • (LoadError)


59
60
61
62
63
64
# File 'lib/import.rb', line 59

def import(feature, base = caller_locations[0].absolute_path)
  path = find_file(feature, base)
  raise LoadError, "cannot load such file -- #{feature}" unless path

  return evaluate(path)
end