123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Formatter::FormatterSet

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Array
Instance Chain:
self, Array
Inherits: Array
  • Object
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

  • BUILTIN_FORMATTERS_FOR_KEYS =
    # File 'lib/rubocop/formatter/formatter_set.rb', line 11
    {
      '[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
  • FORMATTER_APIS =
    # File 'lib/rubocop/formatter/formatter_set.rb', line 31
    %i[started finished].freeze

Class Method Summary

Instance Method Summary

Constructor Details

.new(options = {}) ⇒ FormatterSet

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 39

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

Instance Method Details

#add_formatter(formatter_type, output_path = nil)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 55

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

#builtin_formatter_class(specified_key) (private)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 86

def builtin_formatter_class(specified_key)
  matching_keys = BUILTIN_FORMATTERS_FOR_KEYS.keys.select do |key|
    /^\[#{specified_key}\]/.match?(key) || specified_key == key.delete('[]')
  end

  raise %(No formatter for "#{specified_key}") if matching_keys.empty?

  raise %(Cannot determine formatter for "#{specified_key}") if matching_keys.size > 1

  formatter_name = BUILTIN_FORMATTERS_FOR_KEYS[matching_keys.first]
  RuboCop::Formatter.const_get(formatter_name)
end

#close_output_files

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 67

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

#custom_formatter_class(specified_class_name) (private)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 99

def custom_formatter_class(specified_class_name)
  constant_names = specified_class_name.split('::')
  constant_names.shift if constant_names.first.empty?
  constant_names.reduce(Object) do |namespace, constant_name|
    namespace.const_get(constant_name, false)
  end
end

#file_finished(file, offenses)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 50

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

#file_started(file, options)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 44

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

#formatter_class(formatter_type) (private)

[ GitHub ]

  
# File 'lib/rubocop/formatter/formatter_set.rb', line 75

def formatter_class(formatter_type)
  case formatter_type
  when Class
    formatter_type
  when /\A[A-Z]/
    custom_formatter_class(formatter_type)
  else
    builtin_formatter_class(formatter_type)
  end
end