Skip to content

Commit

Permalink
finished configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Jun 13, 2011
1 parent b1daa3b commit a7b7a1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
9 changes: 8 additions & 1 deletion lib/lice.rb
@@ -1,5 +1,12 @@
require "lice/error.rb"
require "lice/error"
require "lice/config"

class Lice

attr_accessor :config

def initialize(config_path = Lice::Config::DEFAULT_PATH)
@config = Lice::Config.new(config_path)
end

end
25 changes: 25 additions & 0 deletions lib/lice/config.rb
@@ -0,0 +1,25 @@
require 'yaml'
require 'ostruct'

class Lice

class Config < OpenStruct

DEFAULT_PATH = File.join(Dir.pwd, 'config.yml')

DEFAULTS = {
:source => Dir.pwd,
:destination => File.join(Dir.pwd, '_site')
}

def initialize(path = DEFAULT_PATH)
begin
data = YAML.load_file(path)
rescue Exception => ex
raise Lice::Error.new(ex.message)
end
raise Lice::Error.new("Invalid config file - #{@path}") unless data.is_a? Hash
super(data.merge(DEFAULTS))
end
end
end
3 changes: 3 additions & 0 deletions test/files/array_config.yml
@@ -0,0 +1,3 @@
- one
- two
- three
3 changes: 3 additions & 0 deletions test/files/empty_config.yml
@@ -0,0 +1,3 @@
--- {}


21 changes: 16 additions & 5 deletions test/lice/config_test.rb
Expand Up @@ -4,16 +4,27 @@ module Test
module Lice

class Test::Lice::Config < Assertor::Case
def initialize()
@path = File.join(Dir.pwd, 'config.yml')
def test_no_file_should_raise_error
default_path = File.join(Dir.pwd, 'config.yml')
assert_raises(::Lice::Error, "No such file or directory - #{default_path}") do
::Lice.new()
end
end

def test_no_file_should_thow_error
assert_raises(::Lice::Error, "config file not found in #{@path}") do
::Lice.new()
def test_erroneous_file_should_raise_error
erroneous_path = File.join(Dir.pwd, 'test', 'files', 'array_config.yml')
assert_raises(::Lice::Error, "Invalid config file - #{@path}") do
::Lice.new(erroneous_path)
end
end

def test_should_have_default_values
empty_path = File.join(Dir.pwd, 'test', 'files', 'empty_config.yml')
lice = ::Lice.new(empty_path)
assert_equals(Dir.pwd, lice.config.source)
assert_equals(File.join(Dir.pwd, '_site'), lice.config.destination)
end

end

end
Expand Down

0 comments on commit a7b7a1e

Please sign in to comment.