diff --git a/lib/flapjack/inifile.rb b/lib/flapjack/inifile.rb new file mode 100644 index 000000000..03d715750 --- /dev/null +++ b/lib/flapjack/inifile.rb @@ -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 + diff --git a/spec/inifile_spec.rb b/spec/inifile_spec.rb new file mode 100644 index 000000000..72fb411d5 --- /dev/null +++ b/spec/inifile_spec.rb @@ -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 diff --git a/spec/simple.ini b/spec/simple.ini new file mode 100644 index 000000000..0dd2cf14d --- /dev/null +++ b/spec/simple.ini @@ -0,0 +1,6 @@ +[forks] +hello = world +[spoons] +foo = bar +[splades] +bar = baz \ No newline at end of file