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

Modify the config parser to use symbolized key and group names #20

Open
wants to merge 5 commits 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
21 changes: 16 additions & 5 deletions lib/parseconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ def import_config()
end

if group
self.add_to_group(group, var_name, new_value)
self.add_to_group(group.to_sym, var_name.to_sym, new_value)
else
self.add(var_name, new_value)
self.add(var_name.to_sym, new_value)
end

elsif(/^\[(.+)\]$/.match(line).to_a != [])
group = /^\[(.+)\]$/.match(line).to_a[1]
self.add(group, {})
self.add(group.to_sym, {})

end
end
Expand All @@ -103,7 +103,7 @@ def import_config()
#
def get_value(param)
puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \
"config['param'] or config['group']['param'] instead."
"config[:param] or config[:group][:param] instead."
return self.params[param]
end

Expand All @@ -125,7 +125,9 @@ def get_groups()
# This method adds an element to the config object (not the config file)
# By adding a Hash, you create a new group
def add(param_name, value)
param_name = param_name.to_sym
if value.class == Hash
value = symbolize_nested_hash_keys value
if self.params.has_key?(param_name)
if self.params[param_name].class == Hash
self.params[param_name].merge!(value)
Expand All @@ -144,13 +146,22 @@ def add(param_name, value)
self.params[param_name] = value
end
end


def symbolize_nested_hash_keys param
modify_hash_key = lambda do |hash|
return Hash[hash.map {|k,v| [k.to_sym,((v.is_a? Hash) ? modify_hash_key.call(v) : v)]}]
end
modify_hash_key.call(param)
end
# Add parameters to a group. Note that parameters with the same name
# could be placed in different groups
def add_to_group(group, param_name, value)
group = group.to_sym
param_name = param_name.to_sym
if ! self.groups.include?(group)
self.add(group, {})
end
value = symbolize_nested_hash_keys value if value.is_a? Hash
self.params[group][param_name] = value
end

Expand Down
13 changes: 0 additions & 13 deletions tests/files/demo.rb

This file was deleted.

1 change: 0 additions & 1 deletion tests/files/empty.rb

This file was deleted.

8 changes: 0 additions & 8 deletions tests/files/groups.rb

This file was deleted.

5 changes: 0 additions & 5 deletions tests/files/values.rb

This file was deleted.

88 changes: 74 additions & 14 deletions tests/test_parseconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,87 @@

describe 'ruby-parseconfig' do

Dir['./files/*.conf'].each do |config_file|
it "should parse empty config file" do
parse_config = ParseConfig.new("files/#{File.basename('empty.conf')}")
parse_config.params.should == {}
parse_config.groups.should == []
end

it "should parse #{File.basename(config_file)} properly" do
require "./files/#{File.basename(config_file, '.conf')}.rb"
it "should parse the values" do
parse_config = ParseConfig.new("files/#{File.basename('values.conf')}")
parse_config[:foo].should == "bar"
parse_config[:baz].should == "quux"
parse_config[:"123"].should == "456"
end

c = ParseConfig.new(config_file)
it "should return the same values" do
parse_config = ParseConfig.new("files/#{File.basename('values.conf')}")
parse_config.get_value(:foo).should == parse_config[:foo]
parse_config.get_value(:baz).should == parse_config[:baz]
parse_config.get_value(:"123").should == parse_config[:"123"]
end

# Test overall structure
c.params.should == $result
it "should parse the groups" do
parse_config = ParseConfig.new("files/#{File.basename('groups.conf')}")
parse_config.params[:group1][:foo].should == "bar"
parse_config.params[:group2][:baz].should == "quux"
parse_config.groups.count == 2
parse_config.groups.include?(:group1).should == true
parse_config.groups.include?(:group2).should == true
end

# Test individual accessors
c.groups.sort.should == $result.keys.select{|k| $result[k].is_a? Hash}.sort
c.config_file.should == config_file
it "should return the correct hash" do
parse_config = ParseConfig.new("files/#{File.basename('groups.conf')}")
parse_config.params[:group1].should == parse_config.get_value(:group1)
parse_config.params[:group2].should == parse_config.get_value(:group2)
end

$result.keys.each do |k|
c[k].should == $result[k]
c.get_value(k).should == $result[k]
end
it "should parse configuration files with groups" do
parse_config = ParseConfig.new("files/#{File.basename('demo.conf')}")
parse_config.params[:admin_email].should == "root@localhost"
parse_config.params[:listen_ip].should == "127.0.0.1"
parse_config.params[:listen_port].should == "87345"
parse_config.params[:group1][:user_name].should == "johnny"
parse_config.params[:group1][:group_name].should == "daemons"
parse_config.params[:group2][:user_name].should == "rita"
parse_config.params[:group2][:group_name].should == "daemons"
parse_config.groups.count.should == 2
parse_config.groups.include?(:group1).should == true
parse_config.groups.include?(:group2).should == true
end

end


describe "adding configuration values and groups" do
before :each do
@parse_config = ParseConfig.new
end
it "should add given key and value" do
@parse_config.add("foo", 1)
@parse_config.params[:foo].should == 1
end
it "should add nested hashes" do
@parse_config.add("foo", {"bar" => 123})
@parse_config[:foo][:bar].should == 123
end
it "should add values to existing groups" do
@parse_config.add("foo", {"bar" => 123})
@parse_config.add_to_group("foo","buzz", 345)
@parse_config[:foo][:bar].should == 123
@parse_config[:foo][:buzz].should == 345
@parse_config[:foo].count == 2
end
it "should add nested hash into the group" do
@parse_config.add("foo", {"bar" => 123})
@parse_config.add_to_group("foo","buzz", {"fizz" => 345})
@parse_config[:foo][:bar].should == 123
@parse_config[:foo][:buzz][:fizz].should == 345
@parse_config[:foo].count == 2
end
it "should create a new group and add value" do
@parse_config.add_to_group("foo","buzz", {"fizz" => 345})
@parse_config[:foo][:buzz][:fizz].should == 345
end
end

end