Class: Emfrp::FileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/emfrp/interpreter/file_loader.rb

Constant Summary collapse

FileLoadError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(include_dirs) ⇒ FileLoader

Returns a new instance of FileLoader.



5
6
7
8
# File 'lib/emfrp/interpreter/file_loader.rb', line 5

def initialize(include_dirs)
  @include_dirs = include_dirs
  @loaded_hash = {}
end

Instance Method Details

#add_to_loaded(path, src) ⇒ Object



29
30
31
# File 'lib/emfrp/interpreter/file_loader.rb', line 29

def add_to_loaded(path, src)
  @loaded_hash[path] = [src, path]
end

#get_src_from_full_path(required_full_path) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/emfrp/interpreter/file_loader.rb', line 19

def get_src_from_full_path(required_full_path)
  @loaded_hash.each do |path, x|
    src_str, full_path = *x
    if full_path == required_full_path
      return src_str
    end
  end
  raise "#{required_full_path} is not found"
end

#load(path) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/emfrp/interpreter/file_loader.rb', line 33

def load(path)
  path_str = path.is_a?(Array) ? path.join("/") : path
  if path =~ /^\/.*?/ && File.exist?(path)
    src_str = File.open(path, 'r'){|f| f.read}
    return @loaded_hash[path] = [src_str, path]
  end
  @include_dirs.each do |d|
    full_path = File.expand_path(d + path_str)
    if File.exist?(full_path) && File.ftype(full_path) == "file"
      src_str = File.open(full_path, 'r'){|f| f.read}
      return @loaded_hash[path] = [src_str, full_path]
    elsif File.exist?(full_path + ".mfrp") && File.ftype(full_path + ".mfrp") == "file"
      src_str = File.open(full_path + ".mfrp", 'r'){|f| f.read}
      return @loaded_hash[path] = [src_str, full_path + ".mfrp"]
    end
  end
  raise FileLoadError.new("Cannot load #{path_str}")
end

#loaded?(path) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/emfrp/interpreter/file_loader.rb', line 10

def loaded?(path)
  @loaded_hash.has_key?(path)
end

#loaded_full_path(path) ⇒ Object



14
15
16
17
# File 'lib/emfrp/interpreter/file_loader.rb', line 14

def loaded_full_path(path)
  raise "assertion error" unless loaded?(path)
  @loaded_hash[path][1]
end