Skip to content

Commit

Permalink
added inifile parser, specs, and example ini for spec
Browse files Browse the repository at this point in the history
  • Loading branch information
auxesis committed Nov 25, 2009
1 parent 6036830 commit b99f370
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/flapjack/inifile.rb
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

52 changes: 52 additions & 0 deletions spec/inifile_spec.rb
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
6 changes: 6 additions & 0 deletions spec/simple.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[forks]
hello = world
[spoons]
foo = bar
[splades]
bar = baz

0 comments on commit b99f370

Please sign in to comment.