Class: Yay::Loader
- Inherits:
-
Object
- Object
- Yay::Loader
- Defined in:
- lib/yay/loader.rb
Overview
resolves the location of an include file
Class Method Summary collapse
-
.default_file_loader ⇒ Object
get the default loader.
Instance Method Summary collapse
-
#get_potential_resolutions(filename) ⇒ Object
get the potential target paths.
- #get_rules ⇒ Object
-
#initialize(filename) ⇒ Loader
constructor
A new instance of Loader.
-
#load ⇒ Object
load a file.
-
#parse_file(full_path) ⇒ Object
parse a file.
-
#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.
-
#resolve_file(filename) ⇒ Object
try to determine the location of our file.
Constructor Details
#initialize(filename) ⇒ Loader
Returns a new instance of Loader.
6 7 8 |
# File 'lib/yay/loader.rb', line 6 def initialize filename @filename = filename end |
Class Method Details
Instance Method Details
#get_potential_resolutions(filename) ⇒ Object
get the potential target paths
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/yay/loader.rb', line 33 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_rules ⇒ Object
66 67 68 |
# File 'lib/yay/loader.rb', line 66 def get_rules return @rules end |
#load ⇒ Object
load a file
59 60 61 62 63 64 |
# File 'lib/yay/loader.rb', line 59 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
26 27 28 29 30 |
# File 'lib/yay/loader.rb', line 26 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
18 19 20 21 22 23 |
# File 'lib/yay/loader.rb', line 18 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
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/yay/loader.rb', line 46 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 |