config switched to hash, providing alterable version for writting to settings file

This commit is contained in:
John Bender 2010-01-23 23:38:31 -08:00
parent 5b29ce59aa
commit 2883130434
4 changed files with 31 additions and 21 deletions

View File

@ -1,27 +1,16 @@
module Hobo
module_function
def config
@@config
end
def config_from_hash!(hash)
@@config = Config.from_hash(hash)
def alterable_config
@@alterable_config
end
class Config
class << self
def from_hash(value)
return value unless value.instance_of?(Hash)
result = value.inject({}) do |acc, pair|
acc[pair.first] = from_hash(pair.last)
acc
end
OpenStruct.new(result)
end
end
def set_config!(hash)
@@alterable_config = hash.dup
@@config = hash.freeze
end
end

View File

@ -23,7 +23,7 @@ module Hobo
ensure_directories
ensure_files
parsed = yield(CONFIG.keys.first)
Hobo.config_from_hash!(parsed)
Hobo.set_config!(parsed)
end
end
end

View File

@ -3,9 +3,30 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
class ConfigTest < Test::Unit::TestCase
context "Hobo configuration" do
test "a hash source is converted to dot methods" do
Hobo.config_from_hash!(:a => {:b => 1})
assert_equal Hobo.config.a.b, 1
setup do
@settings = {:a => { :b => 1}}
Hobo.set_config!(@settings)
end
should "prevent alteration after initial creation" do
assert_raise TypeError do
Hobo.config[:a] = 1
end
end
should "leave the actual config unaltered when changing the alterable version" do
Hobo.alterable_config[:a] = 1
assert_equal Hobo.config, @settings
end
should "ensure that the alterable config and config match initially" do
assert_equal Hobo.config, Hobo.alterable_config
end
# TODO of debatable usefulness this test is
should "allow for the alteration of the config" do
Hobo.alterable_config[:a] = 1
assert_not_equal Hobo.alterable_config, Hobo.config
end
end
end

View File

@ -50,7 +50,7 @@ class EnvTest < Test::Unit::TestCase
{ :setting => 1 }
end
assert_equal Hobo.config.setting, 1
assert_equal Hobo.config[:setting], 1
end
end
end