Commit be44192196257b5188c870593d6d05ffc8f3c1d0

Authored by binarylogic
2 parents 2c806c14 3cc435ff
Exists in master

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

lib/settingslogic/settings.rb
... ... @@ -40,10 +40,13 @@ module Settingslogic
40 40 root_path = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/config/" : ""
41 41 file_path = name_or_hash.is_a?(Symbol) ? "#{root_path}#{name_or_hash}.yml" : name_or_hash
42 42 self.update YAML.load(ERB.new(File.read(file_path)).result).to_hash
43   - self.update self[RAILS_ENV] if defined?(RAILS_ENV)
44 43 else
45 44 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.")
46 45 end
  46 + if defined?(RAILS_ENV)
  47 + rails_env = self.keys.include?(RAILS_ENV) ? RAILS_ENV : RAILS_ENV.to_sym
  48 + self.update self[rails_env] if self[rails_env]
  49 + end
47 50 define_settings!
48 51 end
49 52  
... ... @@ -66,6 +69,9 @@ module Settingslogic
66 69 def #{key}
67 70 @#{key} ||= self[#{key.inspect}]
68 71 end
  72 + def #{key}=(value)
  73 + @#{key} = value
  74 + end
69 75 end_eval
70 76 end
71 77 end
... ...
test/application3.yml 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +neat:
  2 + cool:
  3 + awesome: <%= "Ben" + "Johnson" %>
  4 +
  5 +silly: 5
  6 +fun: <%= 5 * 5 %>
  7 +
  8 +test:
  9 + silly: "test_specific"
... ...
test/setting_test.rb
... ... @@ -40,4 +40,24 @@ class TestSetting &lt; Test::Unit::TestCase
40 40 assert_equal 5, settings1.silly
41 41 assert_equal 25, settings1.fun
42 42 end
  43 +
  44 + def test_environment_specific_settings
  45 + in_test_environment do
  46 + settings1 = Settings.new(File.dirname(__FILE__) + '/application3.yml')
  47 + assert_equal 25, settings1.fun
  48 + assert_equal "test_specific", settings1.silly
  49 + end
  50 + end
  51 +
  52 + def test_environment_specific_settings_when_initialized_with_hash
  53 + in_test_environment do
  54 + settings1 = Settings.new(
  55 + :silly => 5,
  56 + 'fun' => 25,
  57 + :test => { :silly => "test_specific" }
  58 + )
  59 + assert_equal 25, settings1.fun
  60 + assert_equal "test_specific", settings1.silly
  61 + end
  62 + end
43 63 end
44 64 \ No newline at end of file
... ...
test/test_helper.rb
... ... @@ -18,4 +18,10 @@ class Test::Unit::TestCase
18 18 configure
19 19 Settingslogic::Settings.reset!
20 20 end
  21 +
  22 + def in_test_environment(&block)
  23 + Settingslogic::Settings.const_set :RAILS_ENV, "test"
  24 + block.call
  25 + Settingslogic::Settings.send :remove_const, :RAILS_ENV
  26 + end
21 27 end
22 28 \ No newline at end of file
... ...