Permalink
Browse files

Merge branch 'master' of git@github.com:binarylogic/settingslogic

  • Loading branch information...
2 parents 2c806c1 + 3cc435f commit be44192196257b5188c870593d6d05ffc8f3c1d0 @binarylogic binarylogic committed Jun 28, 2009
Showing with 42 additions and 1 deletion.
  1. +7 −1 lib/settingslogic/settings.rb
  2. +9 −0 test/application3.yml
  3. +20 −0 test/setting_test.rb
  4. +6 −0 test/test_helper.rb
@@ -40,10 +40,13 @@ def initialize(name_or_hash = Config.settings_file)
root_path = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/" : ""
file_path = name_or_hash.is_a?(Symbol) ? "#{root_path}#{name_or_hash}.yml" : name_or_hash
self.update YAML.load(ERB.new(File.read(file_path)).result).to_hash
- self.update self[RAILS_ENV] if defined?(RAILS_ENV)
else
raise ArgumentError.new("Your settings must be a hash, a symbol representing the name of the .yml file in your config directory, or a string representing the abosolute path to your settings file.")
end
+ if defined?(RAILS_ENV)
+ rails_env = self.keys.include?(RAILS_ENV) ? RAILS_ENV : RAILS_ENV.to_sym
+ self.update self[rails_env] if self[rails_env]
+ end
define_settings!
end
@@ -66,6 +69,9 @@ def #{key}
def #{key}
@#{key} ||= self[#{key.inspect}]
end
+ def #{key}=(value)
+ @#{key} = value
+ end
end_eval
end
end
View
@@ -0,0 +1,9 @@
+neat:
+ cool:
+ awesome: <%= "Ben" + "Johnson" %>
+
+silly: 5
+fun: <%= 5 * 5 %>
+
+test:
+ silly: "test_specific"
View
@@ -40,4 +40,24 @@ def test_initialized_with_hash
assert_equal 5, settings1.silly
assert_equal 25, settings1.fun
end
+
+ def test_environment_specific_settings
+ in_test_environment do
+ settings1 = Settings.new(File.dirname(__FILE__) + '/application3.yml')
+ assert_equal 25, settings1.fun
+ assert_equal "test_specific", settings1.silly
+ end
+ end
+
+ def test_environment_specific_settings_when_initialized_with_hash
+ in_test_environment do
+ settings1 = Settings.new(
+ :silly => 5,
+ 'fun' => 25,
+ :test => { :silly => "test_specific" }
+ )
+ assert_equal 25, settings1.fun
+ assert_equal "test_specific", settings1.silly
+ end
+ end
end
View
@@ -18,4 +18,10 @@ def teardown
configure
Settingslogic::Settings.reset!
end
+
+ def in_test_environment(&block)
+ Settingslogic::Settings.const_set :RAILS_ENV, "test"
+ block.call
+ Settingslogic::Settings.send :remove_const, :RAILS_ENV
+ end
end

0 comments on commit be44192

Please sign in to comment.