config switched to hash, providing alterable version for writting to settings file
This commit is contained in:
parent
5b29ce59aa
commit
2883130434
|
@ -1,27 +1,16 @@
|
||||||
module Hobo
|
module Hobo
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def config
|
def config
|
||||||
@@config
|
@@config
|
||||||
end
|
end
|
||||||
|
|
||||||
def config_from_hash!(hash)
|
def alterable_config
|
||||||
@@config = Config.from_hash(hash)
|
@@alterable_config
|
||||||
end
|
end
|
||||||
|
|
||||||
class Config
|
def set_config!(hash)
|
||||||
class << self
|
@@alterable_config = hash.dup
|
||||||
def from_hash(value)
|
@@config = hash.freeze
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ module Hobo
|
||||||
ensure_directories
|
ensure_directories
|
||||||
ensure_files
|
ensure_files
|
||||||
parsed = yield(CONFIG.keys.first)
|
parsed = yield(CONFIG.keys.first)
|
||||||
Hobo.config_from_hash!(parsed)
|
Hobo.set_config!(parsed)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,9 +3,30 @@ require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||||
class ConfigTest < Test::Unit::TestCase
|
class ConfigTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "Hobo configuration" do
|
context "Hobo configuration" do
|
||||||
test "a hash source is converted to dot methods" do
|
setup do
|
||||||
Hobo.config_from_hash!(:a => {:b => 1})
|
@settings = {:a => { :b => 1}}
|
||||||
assert_equal Hobo.config.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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -50,7 +50,7 @@ class EnvTest < Test::Unit::TestCase
|
||||||
{ :setting => 1 }
|
{ :setting => 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal Hobo.config.setting, 1
|
assert_equal Hobo.config[:setting], 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue