Skip to content

Commit

Permalink
config files are now recursively merged
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbottaro committed Oct 3, 2008
1 parent 8293431 commit 44f05af
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -0,0 +1,2 @@
10/03/2008
* recursively merge the configuration hashes
24 changes: 24 additions & 0 deletions README.rdoc
Expand Up @@ -24,6 +24,30 @@ Example development environment config file:
Example production environment config file:
RAILS_ROOT/config/environments/production.yml

==== UPDATE (10/03/2008)
The config files are recursively merged now. Thus the following works:

<tt>RAILS_ROOT/config/app_config.yml:</tt>
servers:
dev: dev.domain.com
test: test.domain.com

<tt>RAILS_ROOT/config/environments/production.yml:</tt>
servers:
test: testing.domain.com
prod: prod.domain.com

<tt>(application code):</tt>
AppConfig.servers.dev # => dev.domain.com
AppConfig.servers.test # => testing.domain.com
AppConfig.servers.prod # => prod.domain.com

Whereas before, the "servers" section of <tt>production.yml</tt> would completely overwrite the "servers" section of <tt>app_config.yml</tt> and you would get:

AppConfig.servers.dev # => nil
AppConfig.servers.test # => testing.domain.com
AppConfig.servers.prod # => prod.domain.com

=== Embedded Ruby (ERB)
Embedded Ruby is allowed in the configuration files. See examples below.

Expand Down
7 changes: 6 additions & 1 deletion lib/app_config.rb
Expand Up @@ -16,7 +16,7 @@ def self.load_files(conf_path_1, conf_path_2 = nil)
conf2 = YAML.load(ERB.new(IO.read(conf_path_2)).result) if conf_path_2 and File.exists?(conf_path_2)
conf2 = {} if !conf2 or conf2.empty?

conf = conf1.merge(conf2)
conf = recursive_merge(conf1, conf2)
(!conf or conf.empty?) ? OpenStruct.new : convert(conf)

end
Expand All @@ -37,5 +37,10 @@ def self.convert(h) #:nodoc:
end
s
end

# Recursively merges hashes. h2 will overwrite h1.
def self.recursive_merge(h1, h2) #:nodoc:
h1.merge(h2){ |k, v1, v2| v2.kind_of?(Hash) ? recursive_merge(v1, v2) : v2 }
end

end
3 changes: 3 additions & 0 deletions test/app_config.yml
@@ -1,2 +1,5 @@
size: 1
server: google.com
emails:
support: support@domain.com
webmaster: web@domain.com
7 changes: 7 additions & 0 deletions test/app_config_test.rb
Expand Up @@ -41,4 +41,11 @@ def test_erb
assert_equal 6, config.computed
end

def test_recursive_merge
config = ApplicationConfig.load_files('test/app_config.yml', 'test/development.yml')
assert_equal 'support@domain.com', config.emails.support
assert_equal 'webmaster@domain.com', config.emails.webmaster
assert_equal 'feedback@domain.com', config.emails.feedback
end

end
3 changes: 3 additions & 0 deletions test/development.yml
Expand Up @@ -3,3 +3,6 @@ computed: <%= 1 + 2 + 3 %>
section:
size: 3
servers: [ {name: yahoo.com}, {name: amazon.com} ]
emails:
webmaster: webmaster@domain.com
feedback: feedback@domain.com

0 comments on commit 44f05af

Please sign in to comment.