Class: Cane::ViolationFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cane/violation_formatter.rb

Overview

Computes a string to be displayed as output from an array of violations computed by the checks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(violations) ⇒ ViolationFormatter

Returns a new instance of ViolationFormatter.



11
12
13
14
15
16
17
18
# File 'lib/cane/violation_formatter.rb', line 11

def initialize(violations)
  @violations = violations.map do |v|
    v.merge(file_and_line: v[:line] ?
      "%s:%i" % v.values_at(:file, :line) :
      v[:file]
    )
  end
end

Instance Attribute Details

#violationsObject (readonly)

Returns the value of attribute violations.



9
10
11
# File 'lib/cane/violation_formatter.rb', line 9

def violations
  @violations
end

Instance Method Details

#to_sObject



20
21
22
23
24
25
26
27
# File 'lib/cane/violation_formatter.rb', line 20

def to_s
  return "" if violations.empty?

  violations.group_by {|x| x[:description] }.map do |d, vs|
    format_group_header(d, vs) +
      format_violations(vs)
  end.join("\n") + "\n\n" + totals + "\n\n"
end