From 248782a2f22eb1fcd2a7b66b0365fd172f19e042 Mon Sep 17 00:00:00 2001 From: Sreenath N Date: Mon, 10 Mar 2014 22:47:04 +0530 Subject: [PATCH 1/5] use symbols instead of string for group names and keys in hash --- lib/parseconfig.rb | 6 +++--- tests/files/demo.rb | 18 +++++++++--------- tests/files/groups.rb | 8 ++++---- tests/files/values.rb | 6 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/parseconfig.rb b/lib/parseconfig.rb index 3dbc642..42f77ca 100644 --- a/lib/parseconfig.rb +++ b/lib/parseconfig.rb @@ -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 diff --git a/tests/files/demo.rb b/tests/files/demo.rb index e3fba80..8399c76 100644 --- a/tests/files/demo.rb +++ b/tests/files/demo.rb @@ -1,13 +1,13 @@ $result = { - "admin_email" => "root@localhost", - "listen_ip" => "127.0.0.1", - "listen_port" => "87345", - "group1" => { - "user_name" => "johnny", - "group_name" => "daemons" + :admin_email => "root@localhost", + :listen_ip => "127.0.0.1", + :listen_port => "87345", + :group1 => { + :user_name => "johnny", + :group_name => "daemons" }, - "group2" => { - "user_name" => "rita", - "group_name" => "daemons" + :group2 => { + :user_name => "rita", + :group_name => "daemons" } } diff --git a/tests/files/groups.rb b/tests/files/groups.rb index 6b8ed5e..0087048 100644 --- a/tests/files/groups.rb +++ b/tests/files/groups.rb @@ -1,8 +1,8 @@ $result = { - "group1" => { - "foo" => "bar" + :group1 => { + :foo => "bar" }, - "group2" => { - "baz" => "quux" + :group2 => { + :baz => "quux" } } diff --git a/tests/files/values.rb b/tests/files/values.rb index 936902f..3ee7475 100644 --- a/tests/files/values.rb +++ b/tests/files/values.rb @@ -1,5 +1,5 @@ $result = { - "foo" => "bar", - "baz" => "quux", - "123" => "456" # note conversion to strings + :foo => "bar", + :baz => "quux", + :"123" => "456" # note conversion to strings } From 9cbc130613e5a0bac69245853fb50ad6ae68eea7 Mon Sep 17 00:00:00 2001 From: Sreenath N Date: Tue, 11 Mar 2014 00:06:54 +0530 Subject: [PATCH 2/5] Modify ability to add values and add groups to support symbolized keys and groups --- lib/parseconfig.rb | 17 ++++++++++++++--- tests/test_parseconfig.rb | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/parseconfig.rb b/lib/parseconfig.rb index 42f77ca..f9cf445 100644 --- a/lib/parseconfig.rb +++ b/lib/parseconfig.rb @@ -104,12 +104,12 @@ def import_config() def get_value(param) puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \ "config['param'] or config['group']['param'] instead." - return self.params[param] + return self.params[param.to_sym] end # This method is a shortcut to accessing the @params variable def [](param) - return self.params[param] + return self.params[param.to_sym] end # This method returns all parameters/groups defined in a config file. @@ -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) @@ -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 diff --git a/tests/test_parseconfig.rb b/tests/test_parseconfig.rb index 405225b..1f57016 100644 --- a/tests/test_parseconfig.rb +++ b/tests/test_parseconfig.rb @@ -21,9 +21,47 @@ c[k].should == $result[k] c.get_value(k).should == $result[k] end + + #Test add support for retrieving value using string and symbol + $result.keys.each do |k| + c[k.to_s].should == $result[k] + c.get_value(k.to_s).should == $result[k] + end 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 From c982e5e51cccf7ce55e45a8d6044b27962ff0f65 Mon Sep 17 00:00:00 2001 From: Sreenath N Date: Tue, 11 Mar 2014 23:42:27 +0530 Subject: [PATCH 3/5] Remove using sort on groups in the tests for compatibility with ruby 1.8 when symbols are there in the groups --- tests/test_parseconfig.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_parseconfig.rb b/tests/test_parseconfig.rb index 1f57016..52cf39d 100644 --- a/tests/test_parseconfig.rb +++ b/tests/test_parseconfig.rb @@ -14,7 +14,9 @@ c.params.should == $result # Test individual accessors - c.groups.sort.should == $result.keys.select{|k| $result[k].is_a? Hash}.sort + groups = c.groups + result_groups = $result.keys.select{|k| $result[k].is_a? Hash} + groups.should == (groups + result_groups).uniq c.config_file.should == config_file $result.keys.each do |k| From 0d5ee99213c438903e78eb8ee8a59e38440e0ae8 Mon Sep 17 00:00:00 2001 From: Sreenath N Date: Wed, 12 Mar 2014 00:59:20 +0530 Subject: [PATCH 4/5] make tests more explicit to document the sceanrios, remove support for string key based value retrieval as it will fail for nested hash --- lib/parseconfig.rb | 6 ++-- tests/test_parseconfig.rb | 68 +++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/lib/parseconfig.rb b/lib/parseconfig.rb index f9cf445..5bbc409 100644 --- a/lib/parseconfig.rb +++ b/lib/parseconfig.rb @@ -103,13 +103,13 @@ def import_config() # def get_value(param) puts "ParseConfig Deprecation Warning: get_value() is deprecated. Use " + \ - "config['param'] or config['group']['param'] instead." - return self.params[param.to_sym] + "config[:param] or config[:group][:param] instead." + return self.params[param] end # This method is a shortcut to accessing the @params variable def [](param) - return self.params[param.to_sym] + return self.params[param] end # This method returns all parameters/groups defined in a config file. diff --git a/tests/test_parseconfig.rb b/tests/test_parseconfig.rb index 52cf39d..80f6a48 100644 --- a/tests/test_parseconfig.rb +++ b/tests/test_parseconfig.rb @@ -3,37 +3,57 @@ describe 'ruby-parseconfig' do - Dir['./files/*.conf'].each do |config_file| - - it "should parse #{File.basename(config_file)} properly" do - require "./files/#{File.basename(config_file, '.conf')}.rb" - - c = ParseConfig.new(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 - # Test overall structure - c.params.should == $result + 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 - # Test individual accessors - groups = c.groups - result_groups = $result.keys.select{|k| $result[k].is_a? Hash} - groups.should == (groups + result_groups).uniq - c.config_file.should == 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 - $result.keys.each do |k| - c[k].should == $result[k] - c.get_value(k).should == $result[k] - end - - #Test add support for retrieving value using string and symbol - $result.keys.each do |k| - c[k.to_s].should == $result[k] - c.get_value(k.to_s).should == $result[k] - end + 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 - end + 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 + 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 + + describe "adding configuration values and groups" do before :each do @parse_config = ParseConfig.new From b9403ee5e6ae15404eb8b60b1ee24c109776dbc0 Mon Sep 17 00:00:00 2001 From: Sreenath N Date: Wed, 12 Mar 2014 01:02:28 +0530 Subject: [PATCH 5/5] Remove the ruby files from test files folder as they are no longer used --- tests/files/demo.rb | 13 ------------- tests/files/empty.rb | 1 - tests/files/groups.rb | 8 -------- tests/files/values.rb | 5 ----- 4 files changed, 27 deletions(-) delete mode 100644 tests/files/demo.rb delete mode 100644 tests/files/empty.rb delete mode 100644 tests/files/groups.rb delete mode 100644 tests/files/values.rb diff --git a/tests/files/demo.rb b/tests/files/demo.rb deleted file mode 100644 index 8399c76..0000000 --- a/tests/files/demo.rb +++ /dev/null @@ -1,13 +0,0 @@ -$result = { - :admin_email => "root@localhost", - :listen_ip => "127.0.0.1", - :listen_port => "87345", - :group1 => { - :user_name => "johnny", - :group_name => "daemons" - }, - :group2 => { - :user_name => "rita", - :group_name => "daemons" - } -} diff --git a/tests/files/empty.rb b/tests/files/empty.rb deleted file mode 100644 index f324999..0000000 --- a/tests/files/empty.rb +++ /dev/null @@ -1 +0,0 @@ -$result = {} diff --git a/tests/files/groups.rb b/tests/files/groups.rb deleted file mode 100644 index 0087048..0000000 --- a/tests/files/groups.rb +++ /dev/null @@ -1,8 +0,0 @@ -$result = { - :group1 => { - :foo => "bar" - }, - :group2 => { - :baz => "quux" - } -} diff --git a/tests/files/values.rb b/tests/files/values.rb deleted file mode 100644 index 3ee7475..0000000 --- a/tests/files/values.rb +++ /dev/null @@ -1,5 +0,0 @@ -$result = { - :foo => "bar", - :baz => "quux", - :"123" => "456" # note conversion to strings -}