Class: Qrpm::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/qrpm/lexer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reldirs, absdirs) ⇒ Lexer

Returns a new instance of Lexer.



7
8
9
10
# File 'lib/qrpm/lexer.rb', line 7

def initialize(reldirs, absdirs)
  @reldirs = reldirs.map { |p| File.expand_path(p) }
  @absdirs = absdirs.map { |p| File.expand_path(p) }
end

Instance Attribute Details

#absdirsObject (readonly)

Returns the value of attribute absdirs.



5
6
7
# File 'lib/qrpm/lexer.rb', line 5

def absdirs
  @absdirs
end

#reldirsObject (readonly)

Returns the value of attribute reldirs.



4
5
6
# File 'lib/qrpm/lexer.rb', line 4

def reldirs
  @reldirs
end

Class Method Details

.load_yaml(file) ⇒ Object

Is actually a kind of lexer



12
13
14
15
# File 'lib/qrpm/lexer.rb', line 12

def self.load_yaml(file) # Is actually a kind of lexer
  text = IO.read(file).sub(/^__END__.*/m, "")
  YAML.load(text)
end

Instance Method Details

#lex(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/qrpm/lexer.rb', line 17

def lex(file)
  yaml = {}
  source = IO.read(file).sub(/^__END__.*/m, "")
  YAML.load(source).each { |k,v|
    if k == "include"
      includes = v.is_a?(String) ? [v] : v
      includes.each { |f| Lexer.load_yaml(search f).each { |k,v| yaml[k] = v } }
    else
      yaml[k] = v
    end
  }
  yaml
end

#search(file) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/qrpm/lexer.rb', line 31

def search(file)
  case file
    when /^\.\.\/(.*)/; reldirs.map { |d| "#{d}/../#$1" }.find { |f| File.exist? f }
    when /^\.\/(.*)/; reldirs.map { |d| "#{d}/#$1" }.find { |f| File.exist? f }
    when /^\//; File.exist?(file) && file
  else
    absdirs.map { |d| "#{d}/#{file}" }.find { |f| File.exist? f }
  end or raise Error, "Can't find #{file}"
end