Class: Rubocop::Formatter::OffenceCountFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rubocop/formatter/offence_count_formatter.rb

Overview

This formatter displays the list of offended cops with a count of how many offences of their kind were found. Ordered by desc offence count

Here's the format:

(26) LineLength (3) OneLineConditional

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started, #initialize

Constructor Details

This class inherits a constructor from Rubocop::Formatter::BaseFormatter

Instance Attribute Details

#offence_countsObject (readonly)

Returns the value of attribute offence_counts.



14
15
16
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 14

def offence_counts
  @offence_counts
end

Instance Method Details

#file_finished(file, offences) ⇒ Object



21
22
23
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 21

def file_finished(file, offences)
  offences.each { |o| @offence_counts[o.cop_name] += 1 }
end

#finished(inspected_files) ⇒ Object



25
26
27
28
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 25

def finished(inspected_files)
  report_summary(inspected_files.count,
                 ordered_offence_counts(@offence_counts))
end

#ordered_offence_counts(offence_counts) ⇒ Object



41
42
43
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 41

def ordered_offence_counts(offence_counts)
  Hash[offence_counts.sort_by { |k, v| v }.reverse]
end

#report_summary(file_count, offence_counts) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 30

def report_summary(file_count, offence_counts)
  output.puts

  offence_count = total_offence_count(offence_counts)
  offence_counts.each do |cop_name, count|
    output.puts "#{count.to_s.ljust(offence_count.to_s.length + 2)}" +
                "#{cop_name}\n"
  end
  output.puts
end

#started(target_files) ⇒ Object



16
17
18
19
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 16

def started(target_files)
  super
  @offence_counts = Hash.new(0)
end

#total_offence_count(offence_counts = {}) ⇒ Object



45
46
47
# File 'lib/rubocop/formatter/offence_count_formatter.rb', line 45

def total_offence_count(offence_counts = {})
  offence_counts.values.inject(0, :+)
end