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.



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

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



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

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

#namesObject



32
33
34
# File 'lib/glark/app/rcfile.rb', line 32

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

#read_line(line) ⇒ Object



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

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



36
37
38
39
# File 'lib/glark/app/rcfile.rb', line 36

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