Browse files

Encapsulate stuff loaded from "_pith/config.rb" in a disposible "Conf…

…ig" object.
  • Loading branch information...
1 parent 13c78c1 commit fca7bb830d6f730487f0beeb4eb269500612a5d5 @mdub committed May 1, 2012
Showing with 76 additions and 53 deletions.
  1. +29 −0 lib/pith/config.rb
  2. +1 −1 lib/pith/input.rb
  3. +12 −26 lib/pith/project.rb
  4. +3 −3 lib/pith/render_context.rb
  5. +31 −0 spec/pith/config_spec.rb
  6. +0 −23 spec/pith/project_spec.rb
View
29 lib/pith/config.rb
@@ -0,0 +1,29 @@
+module Pith
+
+ class Config
+
+ DEFAULT_IGNORE_PATTERNS = ["_*", ".git", ".gitignore", ".svn", ".sass-cache", "*~", "*.sw[op]"].to_set.freeze
+
+ def initialize
+ @ignore_patterns = DEFAULT_IGNORE_PATTERNS.dup
+ end
+
+ attr_reader :ignore_patterns
+ attr_accessor :assume_content_negotiation
+ attr_accessor :assume_directory_index
+
+ def ignore(pattern)
+ ignore_patterns << pattern
+ end
+
+ def helper_module
+ @helper_module ||= Module.new
+ end
+
+ def helpers(&block)
+ helper_module.module_eval(&block)
+ end
+
+ end
+
+end
View
2 lib/pith/input.rb
@@ -37,7 +37,7 @@ def file
#
def ignorable?
@ignorable ||= path.each_filename do |path_component|
- project.ignore_patterns.each do |pattern|
+ project.config.ignore_patterns.each do |pattern|
return true if File.fnmatch(pattern, path_component)
end
end
View
38 lib/pith/project.rb
@@ -1,4 +1,5 @@
require "logger"
+require "pith/config"
require "pith/input"
require "pith/pathname_ext"
require "pith/reference_error"
@@ -9,12 +10,9 @@ module Pith
class Project
- DEFAULT_IGNORE_PATTERNS = ["_*", ".git", ".gitignore", ".svn", ".sass-cache", "*~", "*.sw[op]"].to_set.freeze
-
def initialize(input_dir, output_dir = nil, attributes = {})
@input_dir = Pathname(input_dir)
@output_dir = output_dir ? Pathname(output_dir) : (input_dir + "_out")
- @ignore_patterns = DEFAULT_IGNORE_PATTERNS.dup
@input_map = {}
@output_map = {}
attributes.each do |k,v|
@@ -26,14 +24,6 @@ def initialize(input_dir, output_dir = nil, attributes = {})
attr_reader :input_dir
attr_reader :output_dir
- attr_reader :ignore_patterns
- attr_accessor :assume_content_negotiation
- attr_accessor :assume_directory_index
-
- def ignore(pattern)
- ignore_patterns << pattern
- end
-
# Public: get inputs
#
# Returns Pith::Input objects representing the files in the input_dir.
@@ -84,7 +74,7 @@ def build
# Public: re-sync with the file-system.
#
def sync
- load_config
+ @config = nil
validate_known_inputs
find_new_inputs
end
@@ -113,12 +103,16 @@ def logger
@logger ||= Logger.new(nil)
end
- def helpers(&block)
- helper_module.module_eval(&block)
- end
-
- def helper_module
- @helper_module ||= Module.new
+ def config
+ unless @config
+ @config = Config.new
+ config_file = input_dir + "_pith/config.rb"
+ if config_file.exist?
+ project = @config
+ eval(config_file.read, binding, config_file.to_s, 1)
+ end
+ end
+ @config
end
def config_inputs
@@ -129,14 +123,6 @@ def config_inputs
attr_writer :logger
- def load_config
- config_file = input_dir + "_pith/config.rb"
- project = self
- if config_file.exist?
- eval(config_file.read, binding, config_file.to_s, 1)
- end
- end
-
def load_input(path)
i = Input.new(self, path)
@input_map[path] = i
View
6 lib/pith/render_context.rb
@@ -15,7 +15,7 @@ def initialize(output)
@page = @output.input
@project = @page.project
@input_stack = []
- self.extend(project.helper_module)
+ self.extend(project.config.helper_module)
end
attr_reader :output
@@ -55,8 +55,8 @@ def content_for
def relative_url_to(target_path)
url = target_path.relative_path_from(page.path.parent).to_s
- url = url.sub(/index\.html$/, "") if project.assume_directory_index
- url = url.sub(/\.html$/, "") if project.assume_content_negotiation
+ url = url.sub(/index\.html$/, "") if project.config.assume_directory_index
+ url = url.sub(/\.html$/, "") if project.config.assume_content_negotiation
url = "./" if url.empty?
Pathname(url)
end
View
31 spec/pith/config_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+require 'pith/config'
+
+describe Pith::Config do
+
+ let(:config) { Pith::Config.new }
+
+ describe "#ignore_patterns" do
+
+ it "is a set" do
+ config.ignore_patterns.should be_kind_of(Set)
+ end
+
+ it "includes some sensible defaults" do
+ config.ignore_patterns.should include("_*")
+ config.ignore_patterns.should include(".git")
+ config.ignore_patterns.should include(".svn")
+ end
+
+ end
+
+ describe "#ignore" do
+
+ it "adds to ignore_patterns" do
+ config.ignore("foo")
+ config.ignore_patterns
+ end
+
+ end
+
+end
View
23 spec/pith/project_spec.rb
@@ -110,27 +110,4 @@
end
- describe "#ignore_patterns" do
-
- it "is a set" do
- @project.ignore_patterns.should be_kind_of(Set)
- end
-
- it "includes some sensible defaults" do
- @project.ignore_patterns.should include("_*")
- @project.ignore_patterns.should include(".git")
- @project.ignore_patterns.should include(".svn")
- end
-
- end
-
- describe "#ignore" do
-
- it "adds to ignore_patterns" do
- @project.ignore("foo")
- @project.ignore_patterns
- end
-
- end
-
end

0 comments on commit fca7bb8

Please sign in to comment.