less bad set config value

This commit is contained in:
John Bender 2010-01-29 22:23:33 -08:00
parent dbc10a840f
commit d399a2babf
2 changed files with 10 additions and 6 deletions

View File

@ -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

View File

@ -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