Class: YardJunk::Janitor::BaseReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-junk/janitor/base_reporter.rb

Overview

This class is a base for reporters that could be passed to #report.

Basically, the reporter should define methods:

  • ‘header(title, explanation)` for printing problems section header;

  • ‘row(msg)` for printing instance of Logger::Message;

  • ‘_stats(**statistics)` for printing statistics.

Reporter also could redefine ‘finalize()` method, if it wants to do something at the end of a report (like “add footer and save to file”).

Direct Known Subclasses

HtmlReporter, TextReporter

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ BaseReporter #initialize(filename) ⇒ BaseReporter

Returns a new instance of BaseReporter.

Overloads:

  • #initialize(io) ⇒ BaseReporter

    Parameters:

    • io (#puts)

      Any IO-alike object that defines ‘puts` method.

  • #initialize(filename) ⇒ BaseReporter

    Parameters:

    • filename (String)

      Name of file to save the output.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yard-junk/janitor/base_reporter.rb', line 22

def initialize(io_or_filename = $stdout)
  @io =
    case io_or_filename
    when ->(i) { i.respond_to?(:puts) } # quacks!
      io_or_filename
    when String
      File.open(io_or_filename, 'w')
    else
      fail ArgumentError, "Can't create reporter with #{io_or_filename.class}"
    end
end

Instance Method Details

#finalizeObject



34
# File 'lib/yard-junk/janitor/base_reporter.rb', line 34

def finalize; end

#section(title, explanation, messages) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/yard-junk/janitor/base_reporter.rb', line 36

def section(title, explanation, messages)
  return if messages.empty?

  header(title, explanation)

  messages
    .sort_by { |m| [m.file || '\uFFFF', m.line || 1000, m.message] }
    .each(&method(:row))
end

#stats(**stat) ⇒ Object



46
47
48
# File 'lib/yard-junk/janitor/base_reporter.rb', line 46

def stats(**stat)
  _stats(**stat.merge(duration: humanize_duration(stat[:duration])))
end