confparser 0.0.1.3 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/confparser.rb +39 -4
- metadata +3 -4
data/lib/confparser.rb
CHANGED
@@ -18,7 +18,8 @@ class ConfParser < Hash
|
|
18
18
|
obj.class_eval {
|
19
19
|
attr_reader :parent
|
20
20
|
alias __get__ []
|
21
|
-
|
21
|
+
alias __set__ []=
|
22
|
+
private :__get__, :__set__
|
22
23
|
|
23
24
|
def [] (name)
|
24
25
|
__get__(name).tap {|x|
|
@@ -27,17 +28,40 @@ class ConfParser < Hash
|
|
27
28
|
}.strip if x.is_a?(String)
|
28
29
|
}
|
29
30
|
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
map {|key, value|
|
34
|
+
value.is_a?(Hash) ? "[#{key}]\n#{value.to_s.gsub(/^/, ' ')}" : "#{key} = #{value.to_s}"
|
35
|
+
}.join("\n")
|
36
|
+
end
|
30
37
|
}
|
31
38
|
end
|
32
39
|
end
|
33
40
|
|
34
41
|
class Section < Hash
|
42
|
+
class << self
|
43
|
+
def from_hash (parent, hash)
|
44
|
+
raise ArgumentError unless hash.is_a?(Hash)
|
45
|
+
return hash if hash.is_a?(self)
|
46
|
+
self.new(parent).tap {|sec|
|
47
|
+
hash.each {|key, value|
|
48
|
+
sec[key] = value
|
49
|
+
}
|
50
|
+
}
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
35
54
|
include Template
|
36
55
|
|
37
56
|
def initialize (parent)
|
38
57
|
super()
|
39
58
|
@parent = parent
|
40
59
|
end
|
60
|
+
|
61
|
+
def []= (key, value)
|
62
|
+
raise ArgumentError unless key.is_a?(String) and value.is_a?(String)
|
63
|
+
__set__(key.to_s, value.to_s)
|
64
|
+
end
|
41
65
|
end
|
42
66
|
|
43
67
|
class << self
|
@@ -71,7 +95,7 @@ class ConfParser < Hash
|
|
71
95
|
when /^\s*[;#]/ then next
|
72
96
|
when /^\s*(.+?)\s*[=:]\s*(.*)$/
|
73
97
|
if section
|
74
|
-
|
98
|
+
__set__(section, Section.new(self)) unless self[section]
|
75
99
|
key, self[section][key] = $1, $2
|
76
100
|
else
|
77
101
|
key, self[key] = $1, $2
|
@@ -81,11 +105,11 @@ class ConfParser < Hash
|
|
81
105
|
else
|
82
106
|
if key
|
83
107
|
if section
|
84
|
-
|
108
|
+
__set__(section, Section.new(self)) unless self[section]
|
85
109
|
self[section][key] = '' unless self[section][key]
|
86
110
|
self[section][key] += "\n" + line
|
87
111
|
else
|
88
|
-
|
112
|
+
__set__(key, '') unless self[key]
|
89
113
|
self[key] += "\n" + line
|
90
114
|
end
|
91
115
|
else
|
@@ -95,4 +119,15 @@ class ConfParser < Hash
|
|
95
119
|
}
|
96
120
|
io.close
|
97
121
|
end
|
122
|
+
|
123
|
+
def []= (key, value)
|
124
|
+
raise ArgumentError unless key.is_a?(String) and (value.is_a?(String) or value.is_a?(Hash))
|
125
|
+
__set__(key.to_s, (value.is_a?(String) ? value.to_s : Section.from_hash(self, value)))
|
126
|
+
end
|
127
|
+
|
128
|
+
def save (path)
|
129
|
+
File.open(path, 'w') {|f|
|
130
|
+
f.write(self.to_s)
|
131
|
+
}
|
132
|
+
end
|
98
133
|
end
|
metadata
CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.0.1.3
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- shura
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-
|
17
|
+
date: 2011-09-13 00:00:00 +02:00
|
19
18
|
default_executable:
|
20
19
|
dependencies: []
|
21
20
|
|