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, options = {}) ⇒ ViolationFormatter

Returns a new instance of ViolationFormatter.



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

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

  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cane/violation_formatter.rb', line 22

def to_s
  return "" if violations.empty?

  string = 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"

  if violations.count > options.fetch(:max_violations, 0)
    string = colorize(string)
  end

  string
end