lib/pdd.rb in pdd-0.8 vs lib/pdd.rb in pdd-0.9
- old
+ new
@@ -21,10 +21,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'pdd/sources'
require 'pdd/version'
+require 'pdd/rule/estimates'
+require 'pdd/rule/text'
require 'nokogiri'
require 'logger'
require 'time'
# PDD main module.
@@ -38,10 +40,16 @@
# If it violates XSD schema.
class SchemaError < Error
end
+ RULES = {
+ 'min-estimate' => PDD::Rule::Estimate::Min,
+ 'max-estimate' => PDD::Rule::Estimate::Max,
+ 'min-words' => PDD::Rule::Text::MinWords
+ }
+
# Get logger.
def self.log
unless @log
@log = Logger.new(STDOUT)
@log.formatter = proc { |severity, _, _, msg|
@@ -72,24 +80,26 @@
sources = Sources.new(dir)
@opts[:exclude].each do |p|
sources = sources.exclude(p)
PDD.log.info "excluding #{p}"
end unless @opts[:exclude].nil?
- sanitize(
- 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|
- PDD.log.info "puzzle #{puzzle.props[:ticket]}:" \
- "#{puzzle.props[:estimate]}/#{puzzle.props[:role]}" \
- " at #{puzzle.props[:file]}"
- render puzzle, xml
+ rules(
+ sanitize(
+ 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|
+ PDD.log.info "puzzle #{puzzle.props[:ticket]}:" \
+ "#{puzzle.props[:estimate]}/#{puzzle.props[:role]}" \
+ " at #{puzzle.props[:file]}"
+ render puzzle, xml
+ end
end
end
- end
- end.to_xml
+ end.to_xml
+ )
)
end
private
@@ -115,9 +125,26 @@
xml.puzzle do
props.map do |k, v|
xml.send(:"#{k}", v)
end
end
+ end
+
+ def rules(xml)
+ doc = Nokogiri::XML(xml)
+ total = 0
+ @opts[:rule].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
+ xml
end
def sanitize(xml)
xsd = Nokogiri::XML::Schema(
File.read(File.join(File.dirname(__FILE__), '../assets/puzzles.xsd'))