Module: Apricot::CodeLoader

Defined in:
lib/apricot/code_loader.rb

Class Method Summary collapse

Class Method Details

.find_source(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/apricot/code_loader.rb', line 60

def find_source(path)
  path += ".apr" unless has_extension? path
  path = File.expand_path path if home_path? path

  if qualified_path? path
    if loadable? path
      path
    else
      false
    end
  else
    search_load_path path
  end
end

.has_extension?(path) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/apricot/code_loader.rb', line 84

def has_extension?(path)
  !File.extname(path).empty?
end

.home_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/apricot/code_loader.rb', line 88

def home_path?(path)
  path[0] == '~'
end

.load(path) ⇒ Object

Raises:

  • (LoadError)


13
14
15
16
17
18
19
# File 'lib/apricot/code_loader.rb', line 13

def load(path)
  full_path = find_source(path)
  raise LoadError, "no such file to load -- #{path}" unless full_path

  load_file full_path
  true
end

.load_file(path) ⇒ Object



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
# File 'lib/apricot/code_loader.rb', line 34

def load_file(path)
  compiled_name = Rubinius::ToolSet::Runtime::Compiler.compiled_name(path)

  stat = File::Stat.stat path
  compiled_stat = File::Stat.stat compiled_name

  # Try to load the cached bytecode if it exists and is newer than the
  # source file.
  if stat && compiled_stat && stat.mtime < compiled_stat.mtime
    begin
      code = Rubinius.invoke_primitive :compiledfile_load, compiled_name,
        Rubinius::Signature, Rubinius::RUBY_LIB_VERSION
      usable = true
    rescue Rubinius::Internal
      usable = false
    end

    if usable
      Rubinius.run_script code
      return
    end
  end

  Compiler.compile_and_eval_file path
end

.loadable?(path) ⇒ Boolean

Returns true if the path exists, is a regular file, and is readable.

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/apricot/code_loader.rb', line 98

def loadable?(path)
  @stat = File::Stat.stat path
  return false unless @stat
  @stat.file? and @stat.readable?
end

.loaded?(path) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/apricot/code_loader.rb', line 104

def loaded?(path)
  $LOADED_FEATURES.include?(path)
end

.qualified_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/apricot/code_loader.rb', line 92

def qualified_path?(path)
  # TODO: fix for Windows
  path[0] == '/' || path.prefix?("./") || path.prefix?("../")
end

.require(path) ⇒ Object

Raises:

  • (LoadError)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/apricot/code_loader.rb', line 21

def require(path)
  full_path = find_source(path)
  raise LoadError, "no such file to load -- #{path}" unless full_path

  if loaded? full_path
    false
  else
    load_file full_path
    $LOADED_FEATURES << full_path
    true
  end
end

.search_load_path(path) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/apricot/code_loader.rb', line 75

def search_load_path(path)
  $LOAD_PATH.each do |dir|
    full_path = "#{dir}/#{path}"
    return full_path if loadable? full_path
  end

  false
end