Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hashes for violations, simplify everything.
- Loading branch information
1 parent
f6bab2c
commit 197ac60
Showing
14 changed files
with
84 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,59 @@ | ||
require 'stringio' | ||
require 'ostruct' | ||
|
||
module Cane | ||
|
||
# Computes a string to be displayed as output from an array of violations | ||
# computed by the checks. | ||
class ViolationFormatter < Struct.new(:violations) | ||
def to_s | ||
return '' if violations.empty? | ||
|
||
grouped_violations.map do |description, violations| | ||
format_group_header(description, violations) + | ||
format_violations(violations) | ||
end.flatten.join("\n") + "\n\n" + totals + "\n\n" | ||
class ViolationFormatter | ||
attr_reader :violations | ||
|
||
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 | ||
|
||
protected | ||
def to_s | ||
return "" if violations.empty? | ||
|
||
def totals | ||
"Total Violations: #{violations.count}" | ||
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 | ||
|
||
def format_group_header(description, violations) | ||
["", "%s (%i):" % [description, violations.length], ""] | ||
end | ||
|
||
def format_violations(violations) | ||
column_widths = calculate_columm_widths(violations) | ||
columns = [:file_and_line, :label, :value] | ||
|
||
widths = column_widths(violations, columns) | ||
|
||
violations.map do |violation| | ||
format_violation(violation, column_widths) | ||
violations.map do |v| | ||
format_violation(v, widths) | ||
end | ||
end | ||
|
||
def format_violation(violation, column_widths) | ||
[ | ||
' ' + violation.columns.map.with_index { |column, index| | ||
"%-#{column_widths[index]}s" % column | ||
}.join(' ') | ||
] | ||
def column_widths(violations, columns) | ||
columns.each_with_object({}) do |column, h| | ||
h[column] = violations.map {|v| v[column].to_s.length }.max | ||
end | ||
end | ||
|
||
def calculate_columm_widths(violations) | ||
violations.map { |violation| | ||
violation.columns.map { |x| x.to_s.length } | ||
}.transpose.map(&:max) | ||
def format_violation(v, column_widths) | ||
' ' + column_widths.keys.map {|column| | ||
v[column].to_s.ljust(column_widths[column]) | ||
}.join(' ').strip | ||
end | ||
|
||
def grouped_violations | ||
violations.group_by(&:description) | ||
def totals | ||
"Total Violations: #{violations.length}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters