forked from flapjack/flapjack
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added inifile parser, specs, and example ini for spec
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Nothing Flapjack-specific here - feel free to reuse. | ||
|
||
module Flapjack | ||
class Inifile | ||
|
||
def initialize(string) | ||
@data = {} | ||
|
||
string.split("\n").each do |line| | ||
if line =~ /^\s*\[(.+)\]\s*(;.+)*$/ | ||
@current_section = $1 | ||
@data[@current_section] = {} | ||
end | ||
if line =~ /^\s*(.+)\s*=\s*([^;]+)\s*(;.+)*$/ # after = captures up to ; then groups everything after ; | ||
key = $1.strip | ||
value = $2.strip | ||
@data[@current_section][key] = value | ||
end | ||
end | ||
end | ||
|
||
def [](key) | ||
@data[key] | ||
end | ||
|
||
def keys | ||
@data.keys | ||
end | ||
|
||
def self.read(filename) | ||
self.new(File.read(filename)) | ||
end | ||
|
||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require File.join(File.dirname(__FILE__), '..', 'lib', 'flapjack', 'inifile') | ||
require File.join(File.dirname(__FILE__), 'helpers') | ||
|
||
describe "inifile reader" do | ||
|
||
it "should turn sections into keys" do | ||
example = "[forks]\nhello = world\n[spoons]\nfoo = bar\n[splades]\nbar = baz" | ||
ini = Flapjack::Inifile.new(example) | ||
ini.keys.sort.should == %w{forks splades spoons} | ||
end | ||
|
||
it "should nest parameters under a section" do | ||
example = "[forks]\nhello = world\n[spoons]\nfoo = bar\n[splades]\nbar = baz" | ||
ini = Flapjack::Inifile.new(example) | ||
ini['forks']['hello'].should == "world" | ||
ini['spoons']['foo'].should == "bar" | ||
ini['splades']['bar'].should == "baz" | ||
end | ||
|
||
it "should read a file" do | ||
filename = File.join(File.dirname(__FILE__), 'simple.ini') | ||
ini = Flapjack::Inifile.read(filename) | ||
ini['forks']['hello'].should == "world" | ||
ini['spoons']['foo'].should == "bar" | ||
ini['splades']['bar'].should == "baz" | ||
end | ||
|
||
it "should ignore commented lines" do | ||
example = "[forks]\nhello = world\n[spoons]\nfoo = bar\n# comment goes here\n[splades]\nbar = baz" | ||
ini = Flapjack::Inifile.new(example) | ||
ini['spoons'].keys.include?(/#/).should be_false | ||
ini['spoons'].keys.include?(/comment goes here/).should be_false | ||
ini['spoons'].values.include?(/#/).should be_false | ||
ini['spoons'].values.include?(/comment goes here/).should be_false | ||
end | ||
|
||
it "should ignore blank lines" do | ||
example = "\n\n\n\n\n\n\n[spoons]\n\n\n\n[of]\n[doom]" | ||
ini = Flapjack::Inifile.new(example) | ||
ini.keys.sort.should == %w(doom of spoons) | ||
end | ||
|
||
it "should ignore mid-line comments" do | ||
example = "[forks] ; a comment \nhello = world ; another comment\n\n" | ||
ini = Flapjack::Inifile.new(example) | ||
ini.keys.include?("forks").should be_true | ||
ini['forks']['hello'].should == 'world' | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[forks] | ||
hello = world | ||
[spoons] | ||
foo = bar | ||
[splades] | ||
bar = baz |