Hobo::Config.settings is less crappy than .config ...

This commit is contained in:
John Bender 2010-01-21 23:57:43 -08:00
parent 41a474dd77
commit 9ed95705f7
4 changed files with 20 additions and 17 deletions

View File

@ -1,14 +1,14 @@
module Hobo
class Config
@@config = nil
@@settings = nil
class << self
# TODO Config.config is awkward
def config
@@config
def settings
@@settings
end
def from_hash!(hash)
@@config = hash_to_struct(hash)
@@settings = hash_to_struct(hash)
end
private

View File

@ -1,17 +1,20 @@
module Hobo
class Env
DIRS = [ File.expand_path('~/.hobo') ]
CONFIG_FILE = { File.expand_path('~/.hobo/config.yml') => '/config/default.yml' }
FILES = CONFIG_FILE.merge({}) #additional files go mhia!
HOME = File.expand_path('~/.hobo')
CONFIG = { File.join(HOME, 'config.yml') => '/config/default.yml' }
ENSURE = {
:files => CONFIG.merge({}), #additional files go mhia!
:dirs => [HOME] #additional dirs go mhia!
}
def ensure_directories
DIRS.each do |name|
ENSURE[:dirs].each do |name|
Dir.mkdir(name) unless File.exists?(name)
end
end
def ensure_files
FILES.each do |target, default|
ENSURE[:files].each do |target, default|
File.copy(PROJECT_ROOT + default, target) unless File.exists?(target)
end
end
@ -19,7 +22,7 @@ module Hobo
def load_config
ensure_directories
ensure_files
parsed = yield(CONFIG_FILE.keys.first)
parsed = yield(CONFIG.keys.first)
Config.from_hash!(parsed)
end
end

View File

@ -5,7 +5,7 @@ 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.config.a.b, 1
assert_equal Hobo::Config.settings.a.b, 1
end
end
end

View File

@ -21,19 +21,19 @@ class EnvTest < Test::Unit::TestCase
dir_expectations
file_expectations
@handler.load_config do |file|
assert_equal file, Hobo::Env::CONFIG_FILE.keys.first
assert_equal file, Hobo::Env::CONFIG.keys.first
{ :setting => 1 }
end
assert_equal Hobo::Config.config.setting, 1
assert_equal Hobo::Config.settings.setting, 1
end
end
#TODO Expectations will fail if .hobo dir is present
def dir_expectations
Dir.expects(:mkdir).times(Hobo::Env::DIRS.length).returns nil
Dir.expects(:mkdir).times(Hobo::Env::ENSURE[:dirs].length).returns nil
end
def file_expectations
File.expects(:copy).times(Hobo::Env::FILES.length)
File.expects(:copy).times(Hobo::Env::ENSURE[:files].length)
end
end