Skip to content

Commit

Permalink
Support for environments in yaml files.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbottaro committed Aug 28, 2009
1 parent ae36e08 commit 3259abe
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CHANGELOG
Expand Up @@ -5,4 +5,10 @@
* Packaged as a gem (but still works as a Rails plugin).
* The app config object is now an instance of ApplicationConfiguration.
* NoMethodError raised if you try to access a config element that doesn't exist.
* ApplicationConfiguration#reload! to reread the config files and rebuild the app config object.
* ApplicationConfiguration#reload! to reread the config files and rebuild the app config object.

08/27/2009
* Updated the ClosedStruct class.

08/28/2009
* Added support for "environments" in the YAML files.
30 changes: 30 additions & 0 deletions README.rdoc
Expand Up @@ -55,5 +55,35 @@ You just need to create an initializer that looks something like this.
If you installed this as a Rails plugin instead of a gem, that code is already run for you in
the plugin's init.rb.

=== Environments

Alternatively to splitting out your environments into separate files, you can just have a single file which defines
the application configuration for all environments (much like how databases.yml works). Note if you do this, nested
configurations will not be recursively merged. See example below.

<em>app_config.yml</em>
defaults: &defaults
one: won
two: too
nested:
foo: foo
bar: bar

development:
<<: *defaults
two: new
nested:
foo: bar

_code_
RAILS_ENV # => "development"
::AppConfig = ApplicationConfiguration.new("app_config.yml")
AppConfig.use_environment!(RAILS_ENV)
AppConfig.one # => "won"
AppConfig.two # => "new"
AppConfig.nested.foo # => "bar"
AppConfig.nested.bar # raises NoMethodError because nested configurations are not recursively merged


== Author
Christopher J. Bottaro
8 changes: 8 additions & 0 deletions lib/app_config.rb
Expand Up @@ -25,6 +25,14 @@ def reload!
@config = ClosedStruct.r_new(conf)
end

def use_environment!(environment)
if @config.respond_to?(environment)
@config = @config.send(environment)
else
raise ArgumentError, "environment doesn't exist in app config: #{environment}"
end
end

private

def method_missing(name, *args)
Expand Down
14 changes: 14 additions & 0 deletions test/app_config_test.rb
Expand Up @@ -61,4 +61,18 @@ def test_reload
assert_equal 1, config.size
end

def test_environments
config = ApplicationConfiguration.new('test/environments.yml')
config.use_environment!("development")
assert_equal 2, config.size
assert_equal "google.com", config.server
assert_equal 6, config.computed
assert_equal 3, config.section.size
assert_equal "yahoo.com", config.section.servers[0].name
assert_equal "amazon.com", config.section.servers[1].name
assert_equal "webmaster@domain.com", config.emails.webmaster
assert_equal "feedback@domain.com", config.emails.feedback
assert_raise(NoMethodError){ config.emails.support }
end

end
17 changes: 17 additions & 0 deletions test/environments.yml
@@ -0,0 +1,17 @@
defaults: &defaults
size: 1
server: google.com
emails:
support: support@domain.com
webmaster: web@domain.com

development:
<<: *defaults
size: 2
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 3259abe

Please sign in to comment.