From 3259abe97f924cd8f89b3d7aa3cfc546ae4bf0cb Mon Sep 17 00:00:00 2001 From: cjbottaro Date: Fri, 28 Aug 2009 11:57:44 -0500 Subject: [PATCH] Support for environments in yaml files. --- CHANGELOG | 8 +++++++- README.rdoc | 30 ++++++++++++++++++++++++++++++ lib/app_config.rb | 8 ++++++++ test/app_config_test.rb | 14 ++++++++++++++ test/environments.yml | 17 +++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 test/environments.yml diff --git a/CHANGELOG b/CHANGELOG index 3f68b7f..c81914f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. \ No newline at end of file +* 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. \ No newline at end of file diff --git a/README.rdoc b/README.rdoc index a03baa0..833fce3 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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. + +app_config.yml + 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 \ No newline at end of file diff --git a/lib/app_config.rb b/lib/app_config.rb index 57529d2..963b018 100644 --- a/lib/app_config.rb +++ b/lib/app_config.rb @@ -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) diff --git a/test/app_config_test.rb b/test/app_config_test.rb index 8a2fdcd..60ef19f 100644 --- a/test/app_config_test.rb +++ b/test/app_config_test.rb @@ -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 diff --git a/test/environments.yml b/test/environments.yml new file mode 100644 index 0000000..a84d35d --- /dev/null +++ b/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 \ No newline at end of file