Permalink
Browse files

New methods for configuration

  • Loading branch information...
1 parent 2a386f6 commit d31ed09a11a2c92283d65ebdddb21bcbd919cb22 Hallison Batista committed with Hallison Batista Feb 4, 2010
Showing with 151 additions and 18 deletions.
  1. +56 −18 lib/postview.rb
  2. +95 −0 test/config_test.rb
View
@@ -1,7 +1,5 @@
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
-# Copyright (c) 2009 Hallison Batista
-#
-# Postview - The minimalist blogware and static page generator.
+# = Postview - The minimalist blogware and static page generator
#
# == Configuration
#
@@ -63,32 +61,72 @@ module Postview
autoload :Application, 'postview/application'
autoload :CLI, 'postview/cli'
+ class << self
- # Current application path.
- def self.path
- @path ||= Pathname.new(Dir.pwd).expand_path
- end
+ # Configuration options for post class.
+ def configure(attributes = {}, &block)
+ @config = Config.new(attributes)
+ block_given? ? (yield config) : config
+ end
- # Change current directory path for load all settings from other
- # path.
- def self.path=(pathname)
- @path = Pathname.new(pathname).expand_path
- end
+ # Configuration
+ def config
+ @config ||= Config.new do |default|
+ default.path = "."
+ end
+ end
- # Version
- def self.version
- @version ||= Version.current
- end
+ # Current version
+ def version
+ @version ||= Version.current
+ end
+
+ end # class self
+
+ class Base #:nodoc:
+ def initialize(attributes = {})
+ attributes.each do |attribute, value|
+ send("#{attribute}=", value) if respond_to? "#{attribute}="
+ end
+ end
+ end # class Base
+
+ class Config < Base
+ # Current application path.
+ attr_reader :path
+
+ # Site attributes.
+ attr_reader :site
+
+ # Directories that be used for load files.
+ attr_reader :directories
+
+ # Section names that be used in mapping routes into application.
+ attr_reader :sections
+
+ # Criates a new configuration.
+ def initialize(attributes = {}) #:yields:config
+ super(attributes)
+ yield self if block_given?
+ end
+
+ # Change current directory path for load all settings from other
+ # path.
+ def path=(pathname)
+ @path = Pathname.new(pathname).expand_path
+ end
+ end # class Config
- class Version #:nodoc:
+ class Version < Base #:nodoc:
FILE = Postview::ROOT.join(".version")
attr_accessor :tag, :date, :milestone
attr_reader :timestamp
def initialize(attributes = {})
- attributes.symbolize_keys.instance_variables_set_to(self)
+ super(attributes)
+ @timestamp = attributes[:timestamp]
end
def to_hash
View
@@ -0,0 +1,95 @@
+$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
+
+require 'lib/postview'
+require 'test/unit'
+require 'rack/test'
+require 'test/customizations'
+
+class ConfigTest < Test::Unit::TestCase
+
+ PATH = Pathname.new("#{File.dirname(__FILE__)}/fixtures")
+ def setup
+ @attributes = {
+ :path => PATH.join("blog"),
+ :site => {
+ :title => "Postview",
+ :subtitle => "Post your articles",
+ :author => "Jack Ducklet",
+ :email => "jack.ducklet@example.com",
+ :domain => "jackd.example.com",
+ :directory => "path/to/site",
+ :theme => "mytheme"
+ },
+ :directories => {
+ :posts => "posts",
+ :archive => "posts/archive",
+ :drafts => "posts/drafts",
+ :themes => "themes"
+ },
+ :sections => {
+ :root => "/",
+ :posts => "/posts",
+ :tags => "/tags",
+ :archive => "/archive",
+ :drafts => "/drafts",
+ :search => "/search",
+ :about => "/about"
+ }
+ }
+ Postview.configure @attributes
+ @config = Postview.config
+ end
+
+ should "check configuration" do
+ @attributes.each do |method, values|
+ values.collect do |key, value|
+ assert_equal value, @config.send(method)[key]
+ end
+ end
+ end
+
+ should "build site" do
+ assert_not_nil @settings.build_site
+ assert_not_nil @settings.build_site.find
+ assert_not_nil @settings.build_site.find_in_archive
+ assert_not_nil @settings.build_site.find_in_drafts
+ end
+
+ should "rescue exception and load defaults" do
+ settings = Postview::Settings.load_file("file/not/found.yml")
+ @attributes.each do |method, values|
+ values.collect do |key, value|
+ assert_equal value, @settings.send(method)[key]
+ end
+ end
+ end
+
+ should "rescue exception for empty file, incomplete or null values and load defaults" do
+ settings = Postview::Settings.load_file("#{Postview::ROOT}/test/fixtures/empty.yml")
+ @attributes.each do |method, values|
+ values.collect do |key, value|
+ assert_not_nil settings.send(method)[key]
+ end
+ end
+ settings = Postview::Settings.load_file("#{Postview::ROOT}/test/fixtures/incomplete.yml")
+ @attributes.each do |method, values|
+ values.collect do |key, value|
+ assert_not_nil settings.send(method)[key]
+ end
+ end
+ settings = Postview::Settings.new(:site => nil)
+ @attributes.each do |method, values|
+ values.collect do |key, value|
+ assert_not_nil settings.send(method)[key]
+ end
+ end
+ end
+
+ should "load theme from shared directory" do
+ site_path = Pathname.new("test/fixtures/site").expand_path
+ Postview::path = site_path.join("blog")
+ assert_equal site_path.join("themes"), Postview::Settings.load.path_to(:themes)
+ end
+
+end
+

0 comments on commit d31ed09

Please sign in to comment.