less bad set config value
This commit is contained in:
parent
dbc10a840f
commit
d399a2babf
|
@ -1,5 +1,6 @@
|
||||||
module Hobo
|
module Hobo
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def config
|
def config
|
||||||
@@config
|
@@config
|
||||||
end
|
end
|
||||||
|
@ -10,15 +11,18 @@ module Hobo
|
||||||
|
|
||||||
def set_config_value(chain, val, cfg=@@config)
|
def set_config_value(chain, val, cfg=@@config)
|
||||||
keys = chain.split('.')
|
keys = chain.split('.')
|
||||||
|
|
||||||
return if keys.empty?
|
return if keys.empty?
|
||||||
|
|
||||||
key = keys.shift.to_sym
|
if keys.length == 1
|
||||||
if keys.empty?
|
# If we're out of keys and the value for this key is not a leaf blow up
|
||||||
raise InvalidSettingAlteration if cfg[key].instance_of?(Hash)
|
raise InvalidSettingAlteration if cfg[keys.first.to_sym].is_a?(Hash)
|
||||||
return cfg[key] = val if keys.empty?
|
|
||||||
|
# set the value and return if the value is a leaf
|
||||||
|
return cfg[keys.first.to_sym] = val
|
||||||
end
|
end
|
||||||
|
|
||||||
set_config_value(keys.join('.'), val, cfg[key])
|
set_config_value(keys[1..-1].join('.'), val, cfg[keys.first.to_sym])
|
||||||
end
|
end
|
||||||
|
|
||||||
class InvalidSettingAlteration < StandardError; end
|
class InvalidSettingAlteration < StandardError; end
|
||||||
|
|
|
@ -14,7 +14,7 @@ class ConfigTest < Test::Unit::TestCase
|
||||||
|
|
||||||
should "prevent the alteration of a non leaf setting value" do
|
should "prevent the alteration of a non leaf setting value" do
|
||||||
assert_raise Hobo::InvalidSettingAlteration do
|
assert_raise Hobo::InvalidSettingAlteration do
|
||||||
Hobo.set_config_value('a', 2)
|
Hobo.set_config_value 'a', 2
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue