Class: Glark::RCFile

Inherits:
Object
  • Object
show all
Defined in:
lib/glark/app/rcfile.rb

Constant Summary collapse

COMMENT_RE =
Regexp.new '\s*#.*'
NAME_VALUE_RE =
Regexp.new '\s*[=:]\s*'

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ RCFile

Returns a new instance of RCFile.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/glark/app/rcfile.rb', line 11

def initialize file
  @values = Array.new

  pn = file.kind_of?(Pathname) ? file : Pathname.new(file)
  
  return unless pn.exist?

  pn.each_line do |line|
    read_line line
  end
end

Instance Method Details

#add(name, value) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/glark/app/rcfile.rb', line 43

def add name, value
  if ary = @values.assoc(name)
    ary << value
  else
    @values << [ name, value ]
  end
end

#namesObject



34
35
36
# File 'lib/glark/app/rcfile.rb', line 34

def names
  @values.collect { |x| x[0] }
end

#read_line(line) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/glark/app/rcfile.rb', line 23

def read_line line
  line.sub! COMMENT_RE, ''
  line.chomp!
  return if line.empty?
  
  name, value = line.split NAME_VALUE_RE
  return unless name && value

  add name, value
end

#values(name) ⇒ Object



38
39
40
41
# File 'lib/glark/app/rcfile.rb', line 38

def values name
  ary = @values.assoc name
  ary && ary[1 .. -1]
end