Class: PDD::Rule::MaxDuplicates

Inherits:
Object
  • Object
show all
Defined in:
lib/pdd/rule/duplicates.rb

Overview

Rule for max duplicates.

Instance Method Summary collapse

Constructor Details

#initialize(xml, max) ⇒ MaxDuplicates

Ctor.

xml

XML with puzzles



10
11
12
13
# File 'lib/pdd/rule/duplicates.rb', line 10

def initialize(xml, max)
  @xml = xml
  @max = max.to_i
end

Instance Method Details

#errorsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pdd/rule/duplicates.rb', line 15

def errors
  @xml
    .xpath('//puzzle')
    .group_by { |p| p.xpath('body/text()').to_s }
    .map do |_, puzzles|
      next nil if puzzles.count <= @max

      "there are #{puzzles.count} duplicate(s) of the same puzzle: " +
        puzzles.map do |p|
          "#{p.xpath('file/text()')}:#{p.xpath('lines/text()')}"
        end.join(', ') +
        ", while maximum #{@max} duplicate is allowed"
    end.compact
end