env and config tested and working

This commit is contained in:
John Bender 2010-01-21 23:38:23 -08:00
parent 07be6f1ac0
commit b054973dc6
8 changed files with 87 additions and 15 deletions

1
config/default.yml Normal file
View File

@ -0,0 +1 @@
:setting: 1

View File

@ -1,5 +1,9 @@
libdir = File.dirname(__FILE__)
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
PROJECT_ROOT = File.join(libdir, '..')
require 'ostruct'
require 'ftools'
require 'hobo/config'
require 'hobo/env'

View File

@ -2,21 +2,22 @@ module Hobo
class Config
@@config = nil
class << self
# TODO Config.config is awkward
def config
@@config
end
def parse!(source)
@@config ||= parse_to_struct(source)
def from_hash!(hash)
@@config = hash_to_struct(hash)
end
private
def parse_to_struct(value)
def hash_to_struct(value)
return value unless value.instance_of?(Hash)
result = value.inject({}) do |acc, pair|
acc[pair.first] = parse_to_struct(pair.last)
acc[pair.first] = hash_to_struct(pair.last)
acc
end

26
lib/hobo/env.rb Normal file
View File

@ -0,0 +1,26 @@
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!
def ensure_directories
DIRS.each do |name|
Dir.mkdir(name) unless File.exists?(name)
end
end
def ensure_files
FILES.each do |target, default|
File.copy(PROJECT_ROOT + default, target) unless File.exists?(target)
end
end
def load_config
ensure_directories
ensure_files
parsed = yield(CONFIG_FILE.keys.first)
Config.from_hash!(parsed)
end
end
end

View File

@ -1,11 +0,0 @@
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.parse!(:a => {:b => 1})
assert Hobo::Config.config.a.b == 1
end
end
end

11
test/hobo/config_test.rb Normal file
View File

@ -0,0 +1,11 @@
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.config.a.b, 1
end
end
end

39
test/hobo/env_test.rb Normal file
View File

@ -0,0 +1,39 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class EnvTest < Test::Unit::TestCase
context "Hobo environment handler" do
setup do
@handler = Hobo::Env.new
end
test "should check for all required directories" do
dir_expectations
@handler.ensure_directories
end
test "should check for all required config files" do
file_expectations
@handler.ensure_files
end
test "should load configuration" do
dir_expectations
file_expectations
@handler.load_config do |file|
assert_equal file, Hobo::Env::CONFIG_FILE.keys.first
{ :setting => 1 }
end
assert_equal Hobo::Config.config.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
end
def file_expectations
File.expects(:copy).times(Hobo::Env::FILES.length)
end
end

View File

@ -20,3 +20,4 @@ rescue LoadError; end
require File.join(File.dirname(__FILE__), '..', 'lib', 'hobo')
require 'contest'
require 'mocha'