Skip to content
This repository was archived by the owner on Jan 16, 2018. It is now read-only.
Open
10 changes: 7 additions & 3 deletions lib/simple_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions simpleconfig.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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<rake>, [">= 0"])
s.add_development_dependency(%q<yard>, [">= 0"])
s.add_development_dependency(%q<mocha>, [">= 0"])
s.add_development_dependency(%q<mocha>, ["~> 0.14"])
s.add_development_dependency(%q<test-unit>, [">= 0"])
else
s.add_dependency(%q<rake>, [">= 0"])
s.add_dependency(%q<yard>, [">= 0"])
Expand Down
28 changes: 28 additions & 0 deletions test/simple_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/yaml_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down