lib/pdd.rb in pdd-0.9 vs lib/pdd.rb in pdd-0.10
- old
+ new
@@ -23,10 +23,11 @@
require 'pdd/sources'
require 'pdd/version'
require 'pdd/rule/estimates'
require 'pdd/rule/text'
+require 'pdd/rule/duplicates'
require 'nokogiri'
require 'logger'
require 'time'
# PDD main module.
@@ -43,35 +44,37 @@
end
RULES = {
'min-estimate' => PDD::Rule::Estimate::Min,
'max-estimate' => PDD::Rule::Estimate::Max,
- 'min-words' => PDD::Rule::Text::MinWords
+ 'min-words' => PDD::Rule::Text::MinWords,
+ 'max-duplicates' => PDD::Rule::MaxDuplicates
}
# Get logger.
def self.log
- unless @log
- @log = Logger.new(STDOUT)
- @log.formatter = proc { |severity, _, _, msg|
- puts "#{severity}: #{msg.dump}"
+ unless @logger
+ @logger = Logger.new(STDOUT)
+ @logger.formatter = proc { |severity, _, _, msg|
+ "#{severity}: #{msg.dump}\n"
}
+ @logger.level = Logger::ERROR
end
- @log
+ @logger
end
class << self
- attr_writer :log
+ attr_writer :logger
end
# Code base abstraction
class Base
# Ctor.
# +opts+:: Options
def initialize(opts)
@opts = opts
- PDD.log = Logger.new(File::NULL) unless @opts.verbose?
+ PDD.log.level = Logger::INFO if @opts.verbose?
PDD.log.info "my version is #{PDD::VERSION}"
end
# Generate XML.
def xml
@@ -80,12 +83,12 @@
sources = Sources.new(dir)
@opts[:exclude].each do |p|
sources = sources.exclude(p)
PDD.log.info "excluding #{p}"
end unless @opts[:exclude].nil?
- rules(
- sanitize(
+ sanitize(
+ rules(
Nokogiri::XML::Builder.new do |xml|
xml << "<?xml-stylesheet type='text/xsl' href='#{xsl}'?>"
xml.puzzles(attrs) do
sources.fetch.each do |source|
source.puzzles.each do |puzzle|
@@ -130,20 +133,19 @@
end
def rules(xml)
doc = Nokogiri::XML(xml)
total = 0
- @opts[:rule].map do |r|
+ (@opts[:rule] || []).push('max-duplicates:1').map do |r|
name, value = r.split(':')
rule = RULES[name]
fail "rule '#{name}' doesn't exist" if rule.nil?
rule.new(doc, value.to_i).errors.each do |e|
- puts e
PDD.log.error e
total += 1
end
- end unless @opts[:rule].nil?
- fail "#{total} errors, see log above" unless total == 0
+ end
+ fail PDD::Error, "#{total} errors, see log above" unless total == 0
xml
end
def sanitize(xml)
xsd = Nokogiri::XML::Schema(