diff --git a/lib/simple_config.rb b/lib/simple_config.rb index 67db1c1..90a6af7 100644 --- a/lib/simple_config.rb +++ b/lib/simple_config.rb @@ -75,7 +75,10 @@ def get(key) # @return The current value for +:key+. def unset(key) singleton_class.send(:remove_method, key) - @settings.delete(key) + setting = @settings.delete(key) + # If there was a setting return that, otherwise continue to try and remove a group which may exist + return setting unless setting.nil? + @groups.delete(key) end # Returns whether a variable with given +key+ is set. @@ -102,7 +105,7 @@ def unset(key) # @param [Symbol] The key to check. # @return [Boolean] True if the key is set. def exists?(key) - @settings.key?(key) + @settings.key?(key) || @groups.key?(key) end def set?(key) @@ -163,7 +166,8 @@ class << self class YAMLParser def initialize(raw_yaml_data) require 'yaml' - @data = YAML.load(raw_yaml_data) + require 'erb' + @data = YAML.load(ERB.new(raw_yaml_data).result(binding)) end def self.parse_contents_of_file(yaml_file) diff --git a/simpleconfig.gemspec b/simpleconfig.gemspec index e4592e3..b8cfa3f 100644 --- a/simpleconfig.gemspec +++ b/simpleconfig.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = "simpleconfig" - s.version = "2.0.1" + s.version = "2.0.2" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Luke Redpath", "Simone Carletti"] @@ -22,7 +22,8 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q, [">= 0"]) s.add_development_dependency(%q, [">= 0"]) - s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, ["~> 0.14"]) + s.add_development_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) diff --git a/test/simple_config_test.rb b/test/simple_config_test.rb index 9e4ad53..17c6240 100644 --- a/test/simple_config_test.rb +++ b/test/simple_config_test.rb @@ -51,6 +51,24 @@ def test_unset_should_return_deleted_value assert_equal('bar', @config.unset(:foo)) end + def test_unset_should_delete_group + assert(!@config.to_hash.key?(:foo)) + @config.group :foo do + set :bar, 'baz' + end + assert(@config.to_hash.key?(:foo)) + @config.unset(:foo) + assert(!@config.to_hash.key?(:foo)) + end + + def test_unset_should_return_deleted_group + assert(!@config.to_hash.key?(:foo)) + @config.group :foo do + set :bar, 'baz' + end + assert_equal(@config.foo, @config.unset(:foo)) + end + def test_exists_should_return_whether_variable_isset assert(!@config.exists?(:foo)) @config.set(:foo, 'bar') @@ -59,6 +77,16 @@ def test_exists_should_return_whether_variable_isset assert(!@config.exists?(:foo)) end + def test_exists_should_return_whether_group_exists + assert(!@config.exists?(:foo)) + @config.group :foo do + set :bar, 'baz' + end + assert(@config.exists?(:foo)) + @config.unset(:foo) + assert(!@config.exists?(:foo)) + end + def test_exists_should_consider_empty_values_as_set [nil, 0, ''].each do |empty_value| @config.set(:foo, empty_value) diff --git a/test/yaml_parser_test.rb b/test/yaml_parser_test.rb index d1f416a..873c2b8 100644 --- a/test/yaml_parser_test.rb +++ b/test/yaml_parser_test.rb @@ -43,6 +43,17 @@ def test_parsing_of_a_nested_group assert_equal 'bar', @config.group1.group2.foo end + + def test_parsing_of_erb + raw_data = <<-EOF + a_test: <%= 6 / 3 %> + b_test: <%= 5 * 5 %> + EOF + parser = YAMLParser.new(raw_data) + parser.parse_into(@config) + assert_equal 2, @config.a_test + assert_equal 25, @config.b_test + end end class YAMLParserFromContentsOfFile < Test::Unit::TestCase