Class: Yay::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/yay/loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Loader

Returns a new instance of Loader.



5
6
7
# File 'lib/yay/loader.rb', line 5

def initialize filename
  @filename = filename
end

Class Method Details

.default_file_loaderObject



9
10
11
# File 'lib/yay/loader.rb', line 9

def self.default_file_loader
	return Yay::Loader.new Yay::Paths::DEFAULT_YAYFILE
end

Instance Method Details

#get_potential_resolutions(filename) ⇒ Object

get the potential target paths



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yay/loader.rb', line 31

def get_potential_resolutions filename
	paths = Yay::Paths.new
	dirs  = paths.yay_paths
	
	result = []
	dirs.each { |path| 
		result.push("#{path}/#{filename}.yay")
	}

	return result
end

#get_rulesObject



64
65
66
# File 'lib/yay/loader.rb', line 64

def get_rules
  return @rules
end

#loadObject

load a file



57
58
59
60
61
62
# File 'lib/yay/loader.rb', line 57

def load
  resolved = resolve_file @filename
  raise Yay::CouldntFindFileError.new @filename, get_potential_resolutions(@filename) unless resolved
  @rules = parse_file resolved
  return @rules
end

#parse_file(full_path) ⇒ Object

parse a file



24
25
26
27
28
# File 'lib/yay/loader.rb', line 24

def parse_file full_path
  file = File.open(full_path, "rb")
  contents = file.read
  return parse_string contents, full_path
end

#parse_string(string, context_name) ⇒ Object

invoking a new parser and process a string of yay commands any variables inside the parser context will not affect our own this is particularly useful when loading files



16
17
18
19
20
21
# File 'lib/yay/loader.rb', line 16

def parse_string string, context_name
  # invoke a new parser and return the rules it finds
  parser = Yay::Parser.new context_name
			parser.allow_include = true
  return parser.parse string
end

#resolve_file(filename) ⇒ Object

try to determine the location of our file



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/yay/loader.rb', line 44

def resolve_file filename
  paths = get_potential_resolutions filename
  paths.each { |file_name| 
    begin
      stat = File.stat(file_name)
      return file_name if stat.readable?
    rescue Errno::ENOENT 
    end
  }
  return nil
end