Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for single line switches in ini files #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ More information about INI files can be found on the [Wikipedia Page](http://en.

### Properties

The basic element contained in an INI file is the property. Every property has
A basic element contained in an INI file is the property. Every property has
a name and a value, delimited by an equals sign *=*. The name appears to the
left of the equals sign and the value to the right.

name=value

### Switches

Switches are sometimes present in INI files to indicate whether a feature
should be on or off. Switches are made up by a single word on a line on its
own.

name

Switches are represented internally as an empty hash `{}` to avoid clashing
with other values and are deactivated by removing the line from the file
completely.

### Sections

Section declarations start with *[* and end with *]* as in `[section1]` and
Expand Down Expand Up @@ -49,6 +61,7 @@ A typical INI file might look like this:
# another comment
var1 = baz
var2 = shoodle
a_switch


Implementation
Expand Down
13 changes: 11 additions & 2 deletions lib/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ def write( opts = {} )
File.open(filename, mode) do |f|
@ini.each do |section,hash|
f.puts "[#{section}]"
hash.each {|param,val| f.puts "#{param} #{@param} #{escape_value val}"}
hash.each {|param,val|
if val.class == Hash and val.empty?
f.puts param
else
f.puts "#{param} #{@param} #{escape_value val}"
end
}
f.puts
end
end
Expand Down Expand Up @@ -428,6 +434,7 @@ def initialize( hash, param, comment, default )
@section_regexp = %r/\A\s*\[([^\]]+)\]#{comment}/
@ignore_regexp = %r/\A#{comment}/
@property_regexp = %r/\A(.*?)(?<!\\)#{param}(.*)\z/
@switch_regexp = %r/\A\s*([\w\.]+)\s*#{comment}/

@open_quote = %r/\A\s*(".*)\z/
@close_quote = %r/\A(.*(?<!\\)")#{comment}/
Expand Down Expand Up @@ -528,12 +535,14 @@ def parse( content )
error if property.empty?

continuation = parse_value $2
when @switch_regexp
#self.property = $1.strip
self.section[$1.strip] = {}
else
error
end
end
end

# check here if we have a dangling value ... usually means we have an
# unmatched open quote
if leading_quote?
Expand Down
3 changes: 3 additions & 0 deletions test/data/switch.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[section_six]
switch_support

14 changes: 14 additions & 0 deletions test/test_inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,5 +557,19 @@ def test_empty_comment_string
assert_equal 3, ini_file['section_one']['one']
assert_equal 5, ini_file['section_five']['five']
end

def test_switch_support_read
ini_file = IniFile.load('test/data/switch.ini')

assert_equal({}, ini_file['section_six']['switch_support'])
end

def test_switch_support_write
ini_file = IniFile.new(:filename => "test/data/tmp.ini")
ini_file["foo"] = {"switch_support" => {}}
ini_file.save

assert File.read("test/data/tmp.ini") =~ /^switch_support$/
end
end