Permalink
Browse files

Extract configuration settings to separate module.

  • Loading branch information...
1 parent b487368 commit 3761a3710ee1fc00c88dcaeb3a6afab8cae7eaf9 @stuartellis committed Nov 7, 2010
Showing with 81 additions and 25 deletions.
  1. +1 −0 lib/proof.rb
  2. +18 −25 lib/proof/application.rb
  3. +32 −0 lib/proof/configuration.rb
  4. +30 −0 spec/proof/application_spec.rb
View
@@ -1,6 +1,7 @@
# Requires items from the main Proof module
require File.join(File.dirname(File.expand_path(__FILE__)), 'proof', 'application')
+require File.join(File.dirname(File.expand_path(__FILE__)), 'proof', 'configuration')
require File.join(File.dirname(File.expand_path(__FILE__)), 'proof', 'report')
require File.join(File.dirname(File.expand_path(__FILE__)), 'proof', 'report_builder')
require File.join(File.dirname(File.expand_path(__FILE__)), 'proof', 'version')
View
@@ -7,25 +7,11 @@ module Proof
module Application
extend self
attr_accessor :config, :output
-
- DEFAULT_TITLE = 'Readability Report'
-
- REPORT_TOTALS = [:num_paragraphs, :num_sentences, :num_words, :num_characters]
-
- SUMMARY_ATTRIBUTES = [:flesch, :fog, :kincaid,
- :num_paragraphs, :num_sentences, :num_words, :num_characters,
- :words_per_sentence, :syllables_per_word]
-
- FORMATS = {
- :condensed => {:template => 'condensed.txt.erb'},
- :full => {:template => 'full.md.erb'},
- :html => {:template => 'full.html.erb'},
- :short => {:template => 'short.md.erb'}
- }
-
+
# Returns the template for the specified format
def get_template(format)
- File.read(File.join('layouts', FORMATS[format][:template]))
+ templates = setting(:formats)
+ File.read(File.join(setting(:layouts_dir), templates[format]))
end
# Returns a list of all of the files from the given sources
@@ -45,7 +31,7 @@ def list_files(sources)
# Parse the provided arguments
def read_arguments(args)
- @config = {} if @config == nil
+ @config = Proof::Configuration::DEFAULTS unless @config
options = OptionParser.new() do |opts|
opts.banner = "Usage: proof [options] FILE1 FILE2 FILE3"
opts.separator ""
@@ -75,11 +61,10 @@ def read_arguments(args)
def report(filenames)
summaries = []
filenames.each do |filename|
- content = File.read(filename)
- summaries << Proof::Content::Analyzer.summarize(filename, content, SUMMARY_ATTRIBUTES)
+ content = File.read(filename)
+ summaries << Proof::Content::Analyzer.summarize(filename, content, setting(:summary_attributes))
end
- @config[:title] = DEFAULT_TITLE unless @config[:title]
- report_builder = Proof::ReportBuilder.new(config[:title], summaries, REPORT_TOTALS)
+ report_builder = Proof::ReportBuilder.new(setting(:report_title), summaries, setting(:report_totals))
report_builder.report()
end
@@ -93,12 +78,20 @@ def run(output=STDOUT, args=ARGV)
else
files = list_files(sources)
report = report(files)
- @config[:format] = :short unless @config[:format]
- template = get_template(@config[:format])
+ template = get_template(setting(:report_format))
@output.puts report.render(template)
exit
end
end
-
+
+ # Returns configuration setting or the default value
+ def setting(key)
+ if @config
+ @config[key] || Proof::Configuration::DEFAULTS[key]
+ else
+ Proof::Configuration::DEFAULTS[key]
+ end
+ end
+
end
end
View
@@ -0,0 +1,32 @@
+# -*- encoding: utf-8 -*-
+
+module Proof
+ module Configuration
+
+ DEFAULTS = {
+ :formats => {
+ :condensed => 'condensed.txt.erb',
+ :full => 'full.md.erb',
+ :html => 'full.html.erb',
+ :short => 'short.md.erb'
+ },
+ :layouts_dir => 'layouts',
+ :report_format => :short,
+ :report_title => 'Readability Report',
+ :report_totals => [
+ :num_paragraphs,
+ :num_sentences,
+ :num_words,
+ :num_characters
+ ],
+ :summary_attributes => [
+ :flesch,
+ :fog, :kincaid,
+ :num_paragraphs, :num_sentences,
+ :num_words, :num_characters,
+ :words_per_sentence, :syllables_per_word
+ ]
+ }
+
+ end
+end
@@ -11,6 +11,10 @@ module Proof
Proof::Application.output = output
end
+ before(:each) do
+ Proof::Application.config = nil
+ end
+
it "builds a list of files for a single directory" do
sources = [File.join('spec', 'files')]
files = Proof::Application.list_files(sources)
@@ -69,7 +73,33 @@ module Proof
end
end
+
+ describe "#setting" do
+
+ it "should return a single default setting" do
+ setting = Proof::Application.setting(:report_title)
+ setting.should == 'Readability Report'
+ end
+
+ it "should return a default Hash setting" do
+ setting = Proof::Application.setting(:formats)
+ setting.should be_kind_of(Hash)
+ end
+ it "should return a single setting from the config hash" do
+ Proof::Application.config = { :test_setting => 'Test Setting' }
+ setting = Proof::Application.setting(:test_setting)
+ setting.should == 'Test Setting'
+ end
+
+ it "should return a Hash from the config hash" do
+ Proof::Application.config = { :test_hash => {:test_setting => 'Test Setting'} }
+ setting_hash = Proof::Application.setting(:test_hash)
+ setting_hash.should include(:test_setting)
+ end
+
+ end
+
end
end

0 comments on commit 3761a37

Please sign in to comment.