Class: Krakken

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

Instance Method Summary collapse

Instance Method Details

#parse(config = "settings.conf") ⇒ Object

The method used to parse the config files



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/krakken.rb', line 4

def parse(config="settings.conf")

  values = []
  
  # Create a new File handler object
  File.open(config, 'r') do |configfile|
    # Keep parsing long as there is another unparsed line in the file
    while (nextline = configfile.gets)
      # Check to see if we are parsing a comment
      if nextline[0] != "#"
        # Remove the whitespace
        nextline = nextline.gsub(/\s+/, '')
        # Split the key from the value
        # And add each to an array
        splitvalues = nextline.split("=")
        splitvalues.each {|object| values << object}
      end
    end
  end
  # Return the parsed values as a hash
  return Hash[*values.flatten]
end

#write(settings, output = "settings.conf") ⇒ Object

The method used to save the config file



28
29
30
31
32
33
34
35
36
# File 'lib/krakken.rb', line 28

def write(settings, output="settings.conf")

  # Create the config file as a string to be written to disk
  config = settings.map{|k,v| "#{k} = #{v}\n"}.join('')

  # And save
  File.open(output, 'w') {|f| f.write(config)}

end