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



27
28
29
30
# File 'lib/pdd/rule/duplicates.rb', line 27

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

Instance Method Details

#errorsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pdd/rule/duplicates.rb', line 32

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