inifile 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ # The list of files that should be ignored by Mr Bones.
2
+ # Lines that start with '#' are comments.
3
+ #
4
+ # A .gitignore file can be used instead by setting it as the ignore
5
+ # file in your Rakefile:
6
+ #
7
+ # PROJ.ignore_file = '.gitignore'
8
+ #
9
+ # For a project with a C extension, the following would be a good set of
10
+ # exclude patterns (uncomment them if you want to use them):
11
+ # *.[oa]
12
+ *~
13
+ *.sw[op]
14
+ announcement.txt
15
+ coverage
16
+ doc
17
+ pkg
@@ -1,3 +1,8 @@
1
+ == 0.3.0 / 2009-12-10
2
+
3
+ * 1 minor enhancement
4
+ - Added a []= operator to the inifile for setting entire sections
5
+
1
6
  == 0.2.0 / 2009-10-27
2
7
 
3
8
  * 1 minor enhancement
@@ -9,7 +9,7 @@ class IniFile
9
9
 
10
10
  # :stopdoc:
11
11
  class Error < StandardError; end
12
- VERSION = '0.2.1'
12
+ VERSION = '0.3.0'
13
13
  # :startdoc:
14
14
 
15
15
  #
@@ -64,7 +64,7 @@ class IniFile
64
64
  def write( filename = nil )
65
65
  @fn = filename unless filename.nil?
66
66
 
67
- ::File.open(@fn, 'w') do |f|
67
+ File.open(@fn, 'w') do |f|
68
68
  @ini.each do |section,hash|
69
69
  f.puts "[#{section}]"
70
70
  hash.each {|param,val| f.puts "#{param} #{@param} #{val}"}
@@ -155,6 +155,16 @@ class IniFile
155
155
  @ini[section.to_s]
156
156
  end
157
157
 
158
+ #
159
+ # call-seq:
160
+ # ini_file[section] = hash
161
+ #
162
+ # Set the hash of parameter/value pairs for the given _section_.
163
+ #
164
+ def []=( section, value )
165
+ @ini[section.to_s] = value
166
+ end
167
+
158
168
  #
159
169
  # call-seq:
160
170
  # has_section?( section )
@@ -268,19 +278,19 @@ class IniFile
268
278
  # Parse the ini file contents.
269
279
  #
270
280
  def parse
271
- return unless ::Kernel.test ?f, @fn
281
+ return unless File.file?(@fn)
272
282
  section = nil
273
283
 
274
- ::File.open(@fn, 'r') do |f|
284
+ File.open(@fn, 'r') do |f|
275
285
  while line = f.gets
276
286
  line = line.chomp
277
287
 
278
288
  case line
279
289
  # ignore blank lines and comment lines
280
- when @rgxp_comment: next
290
+ when @rgxp_comment; next
281
291
 
282
292
  # this is a section declaration
283
- when @rgxp_section: section = @ini[$1.strip]
293
+ when @rgxp_section; section = @ini[$1.strip]
284
294
 
285
295
  # otherwise we have a parameter
286
296
  when @rgxp_param
@@ -9,13 +9,15 @@ rescue LoadError
9
9
  require 'inifile'
10
10
  end
11
11
 
12
+ require 'fileutils'
13
+
12
14
  begin; require 'turn'; rescue LoadError; end
13
15
  require 'test/unit' unless defined? $ZENTEST and $ZENTEST
14
16
 
15
17
  class TestIniFile < Test::Unit::TestCase
16
18
 
17
19
  def setup
18
- @ini_file = ::IniFile.new 'test/data/good.ini'
20
+ @ini_file = IniFile.new 'test/data/good.ini'
19
21
  @contents = [
20
22
  ['section_one', 'one', '1'],
21
23
  ['section_one', 'two', '2'],
@@ -25,33 +27,40 @@ class TestIniFile < Test::Unit::TestCase
25
27
  ['section three', 'six', '6'],
26
28
  ['section_five', 'seven and eight', '7 & 8']
27
29
  ].sort
30
+
31
+ FileUtils.rm_rf "test/data/tmp.ini"
32
+ FileUtils.cp "test/data/good.ini", "test/data/tmp.ini"
33
+ end
34
+
35
+ def teardown
36
+ FileUtils.rm_rf "test/data/tmp.ini"
28
37
  end
29
38
 
30
39
  def test_class_load
31
- ini_file = ::IniFile.load 'test/data/good.ini'
32
- assert_instance_of ::IniFile, ini_file
40
+ ini_file = IniFile.load 'test/data/good.ini'
41
+ assert_instance_of IniFile, ini_file
33
42
 
34
43
  # see if we can parse different style comments
35
- assert_raise(::IniFile::Error) {::IniFile.load 'test/data/comment.ini', :comment => ';'}
44
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/comment.ini', :comment => ';'}
36
45
 
37
- ini_file = ::IniFile.load 'test/data/comment.ini', :comment => '#'
38
- assert_instance_of ::IniFile, ini_file
46
+ ini_file = IniFile.load 'test/data/comment.ini', :comment => '#'
47
+ assert_instance_of IniFile, ini_file
39
48
 
40
49
  # see if we can parse mixed style comments
41
- assert_raise(::IniFile::Error) {::IniFile.load 'test/data/mixed_comment.ini', :comment => '#'}
50
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/mixed_comment.ini', :comment => '#'}
42
51
 
43
- ini_file = ::IniFile.load 'test/data/mixed_comment.ini', :comment => ';#'
44
- assert_instance_of ::IniFile, ini_file
52
+ ini_file = IniFile.load 'test/data/mixed_comment.ini', :comment => ';#'
53
+ assert_instance_of IniFile, ini_file
45
54
 
46
55
  # see if we can parse different style param separators
47
- assert_raise(::IniFile::Error) {::IniFile.load 'test/data/param.ini'}
56
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/param.ini'}
48
57
 
49
- ini_file = ::IniFile.load 'test/data/param.ini', :parameter => ':'
50
- assert_instance_of ::IniFile, ini_file
58
+ ini_file = IniFile.load 'test/data/param.ini', :parameter => ':'
59
+ assert_instance_of IniFile, ini_file
51
60
 
52
61
  # make sure we error out on files with bad lines
53
- assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_1.ini'}
54
- assert_raise(::IniFile::Error) {::IniFile.load 'test/data/bad_2.ini'}
62
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_1.ini'}
63
+ assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_2.ini'}
55
64
  end
56
65
 
57
66
  def test_clone
@@ -123,7 +132,7 @@ class TestIniFile < Test::Unit::TestCase
123
132
  assert_equal @contents, ary.sort
124
133
 
125
134
  ary = []
126
- ::IniFile.new('temp.ini').each {|*args| ary << args}
135
+ IniFile.new('temp.ini').each {|*args| ary << args}
127
136
  assert_equal [], ary
128
137
  end
129
138
 
@@ -139,7 +148,7 @@ class TestIniFile < Test::Unit::TestCase
139
148
  assert_equal expected, ary.sort
140
149
 
141
150
  ary = []
142
- ::IniFile.new('temp.ini').each_section {|section| ary << section}
151
+ IniFile.new('temp.ini').each_section {|section| ary << section}
143
152
  assert_equal [], ary
144
153
  end
145
154
 
@@ -170,7 +179,7 @@ class TestIniFile < Test::Unit::TestCase
170
179
  assert_equal true, @ini_file.has_section?(:section_two)
171
180
  assert_equal false, @ini_file.has_section?(nil)
172
181
 
173
- ini_file = ::IniFile.new 'temp.ini'
182
+ ini_file = IniFile.new 'temp.ini'
174
183
  assert_equal false, ini_file.has_section?('section_one')
175
184
  assert_equal false, ini_file.has_section?('one')
176
185
  assert_equal false, ini_file.has_section?('two')
@@ -205,7 +214,7 @@ class TestIniFile < Test::Unit::TestCase
205
214
  assert_nil @ini_file[nil]
206
215
 
207
216
  expected = {}
208
- ini_file = ::IniFile.new 'temp.ini'
217
+ ini_file = IniFile.new 'temp.ini'
209
218
  assert_equal expected, ini_file['section_one']
210
219
  assert_equal expected, ini_file['one']
211
220
  assert_nil ini_file[nil]
@@ -213,22 +222,22 @@ class TestIniFile < Test::Unit::TestCase
213
222
 
214
223
  def test_initialize
215
224
  # see if we can parse different style comments
216
- #assert_raise(::IniFile::Error) {::IniFile.new 'test/data/comment.ini'}
225
+ #assert_raise(IniFile::Error) {IniFile.new 'test/data/comment.ini'}
217
226
 
218
- ini_file = ::IniFile.new 'test/data/comment.ini', :comment => '#'
227
+ ini_file = IniFile.new 'test/data/comment.ini', :comment => '#'
219
228
  assert_equal true, ini_file.has_section?('section_one')
220
229
 
221
230
  # see if we can parse different style param separators
222
- assert_raise(::IniFile::Error) {::IniFile.new 'test/data/param.ini'}
231
+ assert_raise(IniFile::Error) {IniFile.new 'test/data/param.ini'}
223
232
 
224
- ini_file = ::IniFile.new 'test/data/param.ini', :parameter => ':'
233
+ ini_file = IniFile.new 'test/data/param.ini', :parameter => ':'
225
234
  assert_equal true, ini_file.has_section?('section_one')
226
235
  assert_equal '1', ini_file['section_one']['one']
227
236
  assert_equal '2', ini_file['section_one']['two']
228
237
 
229
238
  # make sure we error out on files with bad lines
230
- assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_1.ini'}
231
- assert_raise(::IniFile::Error) {::IniFile.new 'test/data/bad_2.ini'}
239
+ assert_raise(IniFile::Error) {IniFile.new 'test/data/bad_1.ini'}
240
+ assert_raise(IniFile::Error) {IniFile.new 'test/data/bad_2.ini'}
232
241
  end
233
242
 
234
243
  def test_sections
@@ -239,7 +248,7 @@ class TestIniFile < Test::Unit::TestCase
239
248
 
240
249
  assert_equal expected, @ini_file.sections.sort
241
250
 
242
- ini_file = ::IniFile.new 'temp.ini'
251
+ ini_file = IniFile.new 'temp.ini'
243
252
  assert_equal [], ini_file.sections
244
253
  end
245
254
 
@@ -259,18 +268,67 @@ class TestIniFile < Test::Unit::TestCase
259
268
 
260
269
  def test_write
261
270
  tmp = 'test/data/temp.ini'
262
- ::File.delete tmp if ::Kernel.test(?f, tmp)
271
+ File.delete tmp if Kernel.test(?f, tmp)
263
272
 
264
273
  @ini_file.save tmp
265
- assert_equal true, ::Kernel.test(?f, tmp)
274
+ assert_equal true, Kernel.test(?f, tmp)
266
275
 
267
- ::File.delete tmp if ::Kernel.test(?f, tmp)
276
+ File.delete tmp if Kernel.test(?f, tmp)
268
277
 
269
- ini_file = ::IniFile.new tmp
278
+ ini_file = IniFile.new tmp
270
279
  ini_file.save
271
- assert_nil ::Kernel.test(?s, tmp)
280
+ assert_nil Kernel.test(?s, tmp)
281
+
282
+ File.delete tmp if Kernel.test(?f, tmp)
283
+ end
284
+
285
+ def test_modifies_current_keys
286
+ ini = IniFile.load("test/data/tmp.ini")
287
+ ini["section one"]["one"] = 17
288
+ ini.save
289
+
290
+ assert File.read("test/data/tmp.ini") =~ /one = 17/
291
+ end
292
+
293
+ def test_can_add_key_to_inifile
294
+ ini_file = IniFile.new("test/data/tmp.ini")
295
+ ini_file["new_section"] = {}
296
+ ini_file.save
297
+
298
+ assert File.read("test/data/tmp.ini") =~ /\[new_section\]/
299
+ end
300
+
301
+ def test_adds_correct_key_to_inifile
302
+ ini_file = IniFile.new("test/data/tmp.ini")
303
+ ini_file["foo"] = {}
304
+ ini_file.save
305
+
306
+ assert File.read("test/data/tmp.ini") =~ /\[foo\]/
307
+ end
308
+
309
+ def test_assigns_values_to_inifile
310
+ ini_file = IniFile.new("test/data/tmp.ini")
311
+ ini_file["foo"] = {
312
+ :bar => "baz"
313
+ }
314
+
315
+ assert_equal ini_file["foo"], { :bar => "baz" }
316
+ end
317
+
318
+ def test_assigns_correct_values_to_inifile
319
+ ini_file = IniFile.new("test/data/tmp.ini")
320
+ ini_file["foo"] = {
321
+ :one => "two"
322
+ }
323
+
324
+ assert_equal ini_file["foo"], { :one => "two" }
325
+ end
272
326
 
273
- ::File.delete tmp if ::Kernel.test(?f, tmp)
327
+ def test_assignment_stringifies_key
328
+ ini_file = IniFile.new("test/data/tmp.ini")
329
+ ini_file["foo"] = {:one => :two}
330
+ ini_file[:foo] = {}
331
+ assert_equal ini_file["foo"], {}
274
332
  end
275
333
  end
276
334
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inifile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Pease
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-07 00:00:00 -07:00
12
+ date: 2009-12-10 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.0.0
23
+ version: 1.1.1
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bones
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 3.0.0
33
+ version: 3.2.0
34
34
  version:
35
35
  description: This is a native Ruby package for reading and writing INI files.
36
36
  email: tim.pease@gmail.com
@@ -40,11 +40,10 @@ extensions: []
40
40
 
41
41
  extra_rdoc_files:
42
42
  - History.txt
43
- - Manifest.txt
44
43
  - README.txt
45
44
  files:
45
+ - .gitignore
46
46
  - History.txt
47
- - Manifest.txt
48
47
  - README.txt
49
48
  - Rakefile
50
49
  - lib/inifile.rb
@@ -1,20 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- lib/inifile.rb
6
- tasks/annotations.rake
7
- tasks/doc.rake
8
- tasks/gem.rake
9
- tasks/manifest.rake
10
- tasks/rubyforge.rake
11
- tasks/setup.rb
12
- tasks/svn.rake
13
- tasks/test.rake
14
- test/data/bad_1.ini
15
- test/data/bad_2.ini
16
- test/data/comment.ini
17
- test/data/good.ini
18
- test/data/mixed_comment.ini
19
- test/data/param.ini
20
- test/test_inifile.rb