inifile 1.0.0 → 1.1.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,12 @@
1
+ language: ruby
2
+
3
+ before_install: "gem install bones"
4
+ install: "rake gem:install_dependencies"
5
+
6
+ script: "rake"
7
+
8
+ rvm:
9
+ - 1.8.7
10
+ - 1.9.2
11
+ - jruby
12
+
@@ -1,3 +1,8 @@
1
+ == 1.1.0 / 2012-02-28
2
+
3
+ Enhancements
4
+ - Added support for a default "global" section
5
+
1
6
  == 1.0.0 / 2012-02-27
2
7
 
3
8
  Major Enhancements
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
@@ -12,7 +12,7 @@ class IniFile
12
12
 
13
13
  # :stopdoc:
14
14
  class Error < StandardError; end
15
- VERSION = '1.0.0'
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 => ';' 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
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 => ';' The line comment character(s)
45
- # :parameter => '=' The parameter / value separator
46
- # :encoding => nil The encoding used for read/write (RUBY 1.9)
47
- # :escape => true Whether or not to escape values when reading/writing
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
- raise Error, "parameter encountered before first section" if @_current_section.nil?
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
@@ -1,6 +1,6 @@
1
- ; having a paramater / value pair outside a section is an error
2
- one = 1
3
-
4
1
  [section_one]
5
2
  one = 1
6
3
  two = 2
4
+
5
+ ; the following is not a valid line
6
+ invalid line
@@ -0,0 +1,3 @@
1
+ ; without any section, a default 'global' section will be created
2
+ one = 1
3
+ two = 2
@@ -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.0.0
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: &2157141580 !ruby/object:Gem::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: *2157141580
24
+ version_requirements: *2167250880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bones
27
- requirement: &2157140760 !ruby/object:Gem::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: *2157140760
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\nAll properties must exist within
44
- a section. If the file contains a property\nbefore the first section is declared,
45
- an error will be raised.\n\n### Sections\n\nSection declarations start with *[*
46
- and end with *]* as in `[section1]` and\n`[section2]` shown in the example below.
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
@@ -1,6 +0,0 @@
1
- [section_one]
2
- one = 1
3
- two = 2
4
-
5
- ; the following is not a valid line
6
- invalid line