Class: Nesta::ConfigFile

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pathObject



3
4
5
# File 'lib/nesta/config_file.rb', line 3

def self.path
  File.expand_path('config/config.yml', Nesta::App.root)
end

Instance Method Details

#set_value(key, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nesta/config_file.rb', line 7

def set_value(key, value)
  pattern = /^\s*#?\s*#{key}:.*/
  replacement = "#{key}: #{value}"

  configured = false
  File.open(self.class.path, 'r+') do |file|
    output = ''
    file.each_line do |line|
      if configured
        output << line
      else
        output << line.sub(pattern, replacement)
        configured = true if line =~ pattern
      end
    end
    output << "#{replacement}\n" unless configured
    file.pos = 0
    file.print(output)
    file.truncate(file.pos)
  end
end