diff --git a/lib/hobo/config.rb b/lib/hobo/config.rb index 80c0a230c..e43f767c9 100644 --- a/lib/hobo/config.rb +++ b/lib/hobo/config.rb @@ -1,5 +1,6 @@ module Hobo module_function + def config @@config end @@ -10,15 +11,18 @@ module Hobo def set_config_value(chain, val, cfg=@@config) keys = chain.split('.') + return if keys.empty? - key = keys.shift.to_sym - if keys.empty? - raise InvalidSettingAlteration if cfg[key].instance_of?(Hash) - return cfg[key] = val if keys.empty? + if keys.length == 1 + # If we're out of keys and the value for this key is not a leaf blow up + raise InvalidSettingAlteration if cfg[keys.first.to_sym].is_a?(Hash) + + # set the value and return if the value is a leaf + return cfg[keys.first.to_sym] = val end - set_config_value(keys.join('.'), val, cfg[key]) + set_config_value(keys[1..-1].join('.'), val, cfg[keys.first.to_sym]) end class InvalidSettingAlteration < StandardError; end diff --git a/test/hobo/config_test.rb b/test/hobo/config_test.rb index f9e94b8df..31efa581b 100644 --- a/test/hobo/config_test.rb +++ b/test/hobo/config_test.rb @@ -14,7 +14,7 @@ class ConfigTest < Test::Unit::TestCase should "prevent the alteration of a non leaf setting value" do assert_raise Hobo::InvalidSettingAlteration do - Hobo.set_config_value('a', 2) + Hobo.set_config_value 'a', 2 end end