Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Issue #2 - implemented :override_with option for #use_environment!

  • Loading branch information...
commit 763359395fc622dd0beb8258b3f7c77b630f6d49 1 parent 01ecf27
@cjbottaro authored
View
20 lib/app_config.rb
@@ -21,15 +21,21 @@ def initialize(conf_path_1, conf_path_2 = nil)
def reload!
conf1 = load_conf_file(@conf_path_1)
conf2 = load_conf_file(@conf_path_2)
- conf = recursive_merge(conf1, conf2)
- @config = ClosedStruct.r_new(conf)
+ @config_hash = recursive_merge(conf1, conf2)
+ @config = ClosedStruct.r_new(@config_hash)
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}"
+ def use_environment!(environment, options = {})
+ raise ArgumentError, "environment doesn't exist in app config: #{environment}" \
+ unless @config_hash.has_key?(environment.to_s)
+
+ @config_hash = @config_hash[environment.to_s]
+ @config = @config.send(environment)
+
+ if options[:override_with] and File.exist?(options[:override_with])
+ overriding_config = load_conf_file(options[:override_with])
+ @config_hash = recursive_merge(@config_hash, overriding_config)
+ @config = ClosedStruct.r_new(@config_hash)
end
end
View
27 test/app_config_test.rb
@@ -75,4 +75,31 @@ def test_environments
assert_raise(NoMethodError){ config.emails.support }
end
+ def test_use_environment_override_with
+ config = ApplicationConfiguration.new('test/environments.yml')
+ config.use_environment!("development", :override_with => "test/override_with.yml")
+ assert_equal 10, config.size
+ assert_equal "over.com", config.section.servers[0].name
+ assert_equal "ride.com", config.section.servers[1].name
+ assert_equal "google.com", config.server
+ assert_equal 6, config.computed
+ assert_equal "webmaster@domain.com", config.emails.webmaster
+ assert_equal "feedback@domain.com", config.emails.feedback
+ assert_raise(NoMethodError){ config.emails.support }
+ end
+
+ def test_use_environment_override_with_no_file
+ config = ApplicationConfiguration.new('test/environments.yml')
+ config.use_environment!("development", :override_with => "test/non_existant.yml")
+ 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
View
3  test/override_with.yml
@@ -0,0 +1,3 @@
+size: 10
+section:
+ servers: [ {name: over.com}, {name: ride.com} ]
Please sign in to comment.
Something went wrong with that request. Please try again.