-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.rb
57 lines (46 loc) · 1.32 KB
/
ini.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class IniParser
def initialize(file_path)
@data = {}
@file_path = file_path
parse(file_path)
end
def getValue(section, key)
@data[section][key] if @data[section]
end
def setValue(section, key, value)
if @data[section]
@data[section][key] = value
else
@data[section] = { key => value }
end
end
def sync_ini
File.open(@file_path, 'w') do |file|
@data.each do |section, keys|
file.puts "[#{section}]"
keys.each { |key, value| file.puts "#{key} = #{value}" }
file.puts
end
end
end
private
def parse(file_path)
current_section = nil
File.foreach(file_path) do |line|
line.strip!
next if line.empty? || line.start_with?('#')
if line.start_with?('[') && line.end_with?(']')
current_section = line[1..-2]
@data[current_section] ||= {}
else
key, value = line.split('=', 2).map(&:strip)
@data[current_section][key] = value if current_section
end
end
end
end
def to_b(string)
return true if string.downcase == "true"
return false if string.downcase == "false"
nil
end