inifile 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +12 -0
- data/History.txt +5 -0
- data/README.md +8 -4
- data/lib/inifile.rb +13 -10
- data/test/data/bad_1.ini +3 -3
- data/test/data/global.ini +3 -0
- data/test/test_inifile.rb +15 -2
- metadata +11 -12
- data/test/data/bad_2.ini +0 -6
data/.travis.yml
ADDED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
inifile
|
1
|
+
inifile [![Build Status](https://secure.travis-ci.org/TwP/inifile.png)](http://travis-ci.org/TwP/inifile)
|
2
2
|
=======
|
3
3
|
|
4
4
|
This is a native Ruby package for reading and writing INI files.
|
@@ -22,9 +22,6 @@ left of the equals sign and the value to the right.
|
|
22
22
|
|
23
23
|
name=value
|
24
24
|
|
25
|
-
All properties must exist within a section. If the file contains a property
|
26
|
-
before the first section is declared, an error will be raised.
|
27
|
-
|
28
25
|
### Sections
|
29
26
|
|
30
27
|
Section declarations start with *[* and end with *]* as in `[section1]` and
|
@@ -61,6 +58,13 @@ The format of INI files is not well defined. Several assumptions are made by
|
|
61
58
|
the **inifile** gem when parsing INI files. Most of these assumptions can be
|
62
59
|
modified at, but the defaults are listed below.
|
63
60
|
|
61
|
+
### Global Properties
|
62
|
+
|
63
|
+
If the INI file lacks any section declarations, or if there are properties
|
64
|
+
decalared before the first section, then these properties will be placed into
|
65
|
+
a default "global" section. The name of this section can be configured when
|
66
|
+
creating an `IniFile` instance.
|
67
|
+
|
64
68
|
### Duplicate Properties
|
65
69
|
|
66
70
|
Duplicate properties are allowed in a single section. The last property value
|
data/lib/inifile.rb
CHANGED
@@ -12,7 +12,7 @@ class IniFile
|
|
12
12
|
|
13
13
|
# :stopdoc:
|
14
14
|
class Error < StandardError; end
|
15
|
-
VERSION = '1.
|
15
|
+
VERSION = '1.1.0'
|
16
16
|
# :startdoc:
|
17
17
|
|
18
18
|
#
|
@@ -23,10 +23,11 @@ class IniFile
|
|
23
23
|
# Open the given _filename_ and load the contents of the INI file.
|
24
24
|
# The following _options_ can be passed to this method:
|
25
25
|
#
|
26
|
-
# :comment => ';'
|
27
|
-
# :parameter => '='
|
28
|
-
# :encoding => nil
|
29
|
-
# :escape => true
|
26
|
+
# :comment => ';' The line comment character(s)
|
27
|
+
# :parameter => '=' The parameter / value separator
|
28
|
+
# :encoding => nil The encoding used for read/write (RUBY 1.9)
|
29
|
+
# :escape => true Whether or not to escape values when reading/writing
|
30
|
+
# :default => 'global' Default global section name
|
30
31
|
#
|
31
32
|
def self.load( filename, opts = {} )
|
32
33
|
new(filename, opts)
|
@@ -41,10 +42,11 @@ class IniFile
|
|
41
42
|
# exists and is a regular file, then its contents will be parsed.
|
42
43
|
# The following _options_ can be passed to this method:
|
43
44
|
#
|
44
|
-
# :comment => ';'
|
45
|
-
# :parameter => '='
|
46
|
-
# :encoding => nil
|
47
|
-
# :escape => true
|
45
|
+
# :comment => ';' The line comment character(s)
|
46
|
+
# :parameter => '=' The parameter / value separator
|
47
|
+
# :encoding => nil The encoding used for read/write (RUBY 1.9)
|
48
|
+
# :escape => true Whether or not to escape values when reading/writing
|
49
|
+
# :default => 'global' Default global section name
|
48
50
|
#
|
49
51
|
def initialize( filename, opts = {} )
|
50
52
|
@fn = filename
|
@@ -52,6 +54,7 @@ class IniFile
|
|
52
54
|
@param = opts.fetch(:parameter, '=')
|
53
55
|
@encoding = opts.fetch(:encoding, nil)
|
54
56
|
@escape = opts.fetch(:escape, true)
|
57
|
+
@default = opts.fetch(:default, 'global')
|
55
58
|
@ini = Hash.new {|h,k| h[k] = Hash.new}
|
56
59
|
|
57
60
|
@rgxp_comment = %r/\A\s*\z|\A\s*[#{@comment}]/
|
@@ -411,7 +414,7 @@ private
|
|
411
414
|
def finish_property
|
412
415
|
return unless @_current_param
|
413
416
|
|
414
|
-
|
417
|
+
@_current_section = @ini[@default] if @_current_section.nil?
|
415
418
|
@_current_section[@_current_param] = unescape @_current_value
|
416
419
|
|
417
420
|
@_current_param = nil
|
data/test/data/bad_1.ini
CHANGED
data/test/test_inifile.rb
CHANGED
@@ -57,7 +57,6 @@ class TestIniFile < Test::Unit::TestCase
|
|
57
57
|
|
58
58
|
# make sure we error out on files with bad lines
|
59
59
|
assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_1.ini'}
|
60
|
-
assert_raise(IniFile::Error) {IniFile.load 'test/data/bad_2.ini'}
|
61
60
|
end
|
62
61
|
|
63
62
|
def test_clone
|
@@ -254,7 +253,6 @@ class TestIniFile < Test::Unit::TestCase
|
|
254
253
|
|
255
254
|
# make sure we error out on files with bad lines
|
256
255
|
assert_raise(IniFile::Error) {IniFile.new 'test/data/bad_1.ini'}
|
257
|
-
assert_raise(IniFile::Error) {IniFile.new 'test/data/bad_2.ini'}
|
258
256
|
end
|
259
257
|
|
260
258
|
def test_sections
|
@@ -439,5 +437,20 @@ class TestIniFile < Test::Unit::TestCase
|
|
439
437
|
assert_equal %q{This string \\\\t contains \\\\n no \\\\r special \\\\0 characters!}, escaped['backslash']
|
440
438
|
end
|
441
439
|
|
440
|
+
def test_global_section
|
441
|
+
ini_file = IniFile.load('test/data/global.ini')
|
442
|
+
|
443
|
+
assert_equal %w[global], ini_file.sections
|
444
|
+
assert_equal '1', ini_file['global']['one']
|
445
|
+
assert_equal '2', ini_file['global']['two']
|
446
|
+
end
|
447
|
+
|
448
|
+
def test_default_global_section
|
449
|
+
ini_file = IniFile.load('test/data/global.ini', :default => 'nonce')
|
450
|
+
|
451
|
+
assert_equal %w[nonce], ini_file.sections
|
452
|
+
assert_equal '1', ini_file['nonce']['one']
|
453
|
+
assert_equal '2', ini_file['nonce']['two']
|
454
|
+
end
|
442
455
|
end
|
443
456
|
|
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: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-02-28 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bones-git
|
16
|
-
requirement: &
|
16
|
+
requirement: &2167250880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.2.5
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2167250880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bones
|
27
|
-
requirement: &
|
27
|
+
requirement: &2167250400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 3.7.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2167250400
|
36
36
|
description: ! "Although made popular by Windows, INI files can be used on any system
|
37
37
|
thanks\nto their flexibility. They allow a program to store configuration data,
|
38
38
|
which\ncan then be easily parsed and changed. Two notable systems that use the INI\nformat
|
@@ -40,12 +40,10 @@ description: ! "Although made popular by Windows, INI files can be used on any s
|
|
40
40
|
Page](http://en.wikipedia.org/wiki/INI_file).\n\n### Properties\n\nThe basic element
|
41
41
|
contained in an INI file is the property. Every property has\na name and a value,
|
42
42
|
delimited by an equals sign *=*. The name appears to the\nleft of the equals sign
|
43
|
-
and the value to the right.\n\n name=value\n\
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
The section declaration marks the\nbeginning of a section. All properties after
|
48
|
-
the section declaration will be\nassociated with that section.\n\n### Comments\n\nAll
|
43
|
+
and the value to the right.\n\n name=value\n\n### Sections\n\nSection declarations
|
44
|
+
start with *[* and end with *]* as in `[section1]` and\n`[section2]` shown in the
|
45
|
+
example below. The section declaration marks the\nbeginning of a section. All properties
|
46
|
+
after the section declaration will be\nassociated with that section.\n\n### Comments\n\nAll
|
49
47
|
lines beginning with a semicolon *;* or a number sign *#* are considered\nto be
|
50
48
|
comments. Comment lines are ignored when parsing INI files.\n\n### Example File
|
51
49
|
Format\n\nA typical INI file might look like this:\n\n [section1]\n ; some
|
@@ -59,15 +57,16 @@ extra_rdoc_files:
|
|
59
57
|
- History.txt
|
60
58
|
files:
|
61
59
|
- .gitignore
|
60
|
+
- .travis.yml
|
62
61
|
- History.txt
|
63
62
|
- README.md
|
64
63
|
- Rakefile
|
65
64
|
- lib/inifile.rb
|
66
65
|
- test/data/bad_1.ini
|
67
|
-
- test/data/bad_2.ini
|
68
66
|
- test/data/browscap.ini
|
69
67
|
- test/data/comment.ini
|
70
68
|
- test/data/escape.ini
|
69
|
+
- test/data/global.ini
|
71
70
|
- test/data/good.ini
|
72
71
|
- test/data/merge.ini
|
73
72
|
- test/data/mixed_comment.ini
|