class Emfrp::FileLoader
Constants
- FileLoadError
Public Class Methods
new(include_dirs)
click to toggle source
# File lib/emfrp/interpreter/file_loader.rb, line 5 def initialize(include_dirs) @include_dirs = include_dirs @loaded_hash = {} end
Public Instance Methods
add_to_loaded(path, src)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/emfrp/interpreter/file_loader.rb, line 10 def loaded?(path) @loaded_hash.has_key?(path) end
loaded_full_path(path)
click to toggle source
# 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