diff --git a/lib/hobo/config.rb b/lib/hobo/config.rb index 823b8c7c9..26cfcb010 100644 --- a/lib/hobo/config.rb +++ b/lib/hobo/config.rb @@ -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 diff --git a/lib/hobo/env.rb b/lib/hobo/env.rb index bde4a04f4..41b9bc3f5 100644 --- a/lib/hobo/env.rb +++ b/lib/hobo/env.rb @@ -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 diff --git a/test/hobo/config_test.rb b/test/hobo/config_test.rb index 58463f754..7009c1167 100644 --- a/test/hobo/config_test.rb +++ b/test/hobo/config_test.rb @@ -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 diff --git a/test/hobo/env_test.rb b/test/hobo/env_test.rb index 89ae1d22f..823640c54 100644 --- a/test/hobo/env_test.rb +++ b/test/hobo/env_test.rb @@ -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