Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added merge, merge! and dup methods to SimpleConfig::Config

  • Loading branch information...
commit 439525f150a056b0a127c6fe3554a1ba58f63da5 1 parent 6664094
@andriusch authored
Showing with 57 additions and 14 deletions.
  1. +23 −14 lib/simple_config.rb
  2. +34 −0 test/simple_config_test.rb
View
37 lib/simple_config.rb
@@ -39,6 +39,12 @@ def initialize
@settings = {}
end
+ def dup
+ self.class.new.tap do |duplicate|
+ duplicate.merge!(to_hash)
+ end
+ end
+
def configure(&block)
instance_eval(&block)
end
@@ -111,6 +117,21 @@ def to_hash
hash
end
+ def merge!(hash)
+ hash.each do |key, value|
+ if value.is_a?(Hash)
+ group(key.to_sym).merge!(value)
+ else
+ set(key.to_sym, value)
+ end
+ end
+ self
+ end
+
+ def merge(hash)
+ dup.merge!(hash)
+ end
+
def load(external_config_file, options={})
options = {:if_exists? => false}.merge(options)
@@ -128,7 +149,7 @@ def load(external_config_file, options={})
private
def define_accessor(name, &block)
- singleton_class.class_eval { define_method(name, &block) } unless respond_to?(name)
+ singleton_class.class_eval { define_method(name, &block) } if !respond_to?(name) || exists?(name)
end
def singleton_class
@@ -148,20 +169,8 @@ def self.parse_contents_of_file(yaml_file)
end
def parse_into(config)
- @data.each do |key, value|
- parse(key, value, config)
- end
+ config.merge!(@data)
end
-
- private
- def parse(key, value, config)
- if value.is_a?(Hash)
- group = config.group(key.to_sym)
- value.each { |key, value| parse(key, value, group) }
- else
- config.set(key.to_sym, value)
- end
- end
end
end
View
34 test/simple_config_test.rb
@@ -148,6 +148,40 @@ def test_should_create_hash_for_second_level
assert_equal hash, @config.test_group.to_hash
end
+ def test_dup_creates_deep_clone
+ new_config = cascaded_test_config.dup
+ new_config.configure do
+ group :test_group do
+ set :inner_key, 'new_foo'
+ end
+ set :test, 'new_test'
+ end
+
+ assert_equal 'test-setting', cascaded_test_config.test
+ assert_equal 'some other value', cascaded_test_config.test_group.inner_key
+ assert_equal 'new_test', new_config.test
+ assert_equal 'new_foo', new_config.test_group.inner_key
+ end
+
+ def test_config_merge_hash
+ config = SimpleConfig::Config.new
+ config.merge!(:example => {:foo => 'bar', :baz => 'qux'}, :test => 'foo')
+
+ assert_equal 'foo', config.test
+ assert_equal 'bar', config.example.foo
+ assert_equal 'qux', config.example.baz
+ end
+
+ def test_config_merge_hash_non_destructive
+ new_config = cascaded_test_config.merge(:test_group => {:inner_key => 'bar'}, :test => 'qux')
+
+ assert_equal 'test-setting', cascaded_test_config.test
+ assert_equal 'some other value', cascaded_test_config.test_group.inner_key
+
+ assert_equal 'qux', new_config.test
+ assert_equal 'bar', new_config.test_group.inner_key
+ end
+
private
def cascaded_test_config
SimpleConfig.for :simple_config_test do
Please sign in to comment.
Something went wrong with that request. Please try again.