Class: RuboCop::Formatter::FormatterSet

Inherits:
Array
  • Object
show all
Defined in:
lib/rubocop/formatter/formatter_set.rb

Overview

This is a collection of formatters. A FormatterSet can hold multiple formatter instances and provides transparent formatter API methods which invoke same method of each formatters.

Constant Summary collapse

BUILTIN_FORMATTERS_FOR_KEYS =
{
  '[a]utogenconf' => 'AutoGenConfigFormatter',
  '[c]lang'       => 'ClangStyleFormatter',
  '[e]macs'       => 'EmacsStyleFormatter',
  '[fi]les'       => 'FileListFormatter',
  '[fu]ubar'      => 'FuubarStyleFormatter',
  '[g]ithub'      => 'GitHubActionsFormatter',
  '[h]tml'        => 'HTMLFormatter',
  '[j]son'        => 'JSONFormatter',
  '[ju]nit'       => 'JUnitFormatter',
  '[m]arkdown'    => 'MarkdownFormatter',
  '[o]ffenses'    => 'OffenseCountFormatter',
  '[pa]cman'      => 'PacmanFormatter',
  '[p]rogress'    => 'ProgressFormatter',
  '[q]uiet'       => 'QuietFormatter',
  '[s]imple'      => 'SimpleTextFormatter',
  '[t]ap'         => 'TapFormatter',
  '[w]orst'       => 'WorstOffendersFormatter'
}.freeze
BUILTIN_FORMATTER_NAMES =
BUILTIN_FORMATTERS_FOR_KEYS.keys.map { |key| key.delete('[]') }
FORMATTER_APIS =
%i[started finished].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FormatterSet

Returns a new instance of FormatterSet.



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

def initialize(options = {})
  super()
  @options = options # CLI options
end

Instance Method Details

#add_formatter(formatter_type, output_path = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/formatter/formatter_set.rb', line 56

def add_formatter(formatter_type, output_path = nil)
  if output_path
    dir_path = File.dirname(output_path)
    FileUtils.mkdir_p(dir_path)
    output = File.open(output_path, 'w')
  else
    output = $stdout
  end

  self << formatter_class(formatter_type).new(output, @options)
end

#close_output_filesObject



68
69
70
71
72
# File 'lib/rubocop/formatter/formatter_set.rb', line 68

def close_output_files
  each do |formatter|
    formatter.output.close if formatter.output.is_a?(File)
  end
end

#file_finished(file, offenses) ⇒ Object



51
52
53
54
# File 'lib/rubocop/formatter/formatter_set.rb', line 51

def file_finished(file, offenses)
  each { |f| f.file_finished(file, offenses) }
  offenses
end

#file_started(file, options) ⇒ Object



45
46
47
48
49
# File 'lib/rubocop/formatter/formatter_set.rb', line 45

def file_started(file, options)
  @options = options[:cli_options]
  @config_store = options[:config_store]
  each { |f| f.file_started(file, options) }
end