class RuboCop::Formatter::FormatterSet

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.

Constants

BUILTIN_FORMATTERS_FOR_KEYS
FORMATTER_APIS

Public Class Methods

new(options = {}) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 33
def initialize(options = {})
  @options = options # CLI options
end

Public Instance Methods

add_formatter(formatter_type, output_path = nil) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 48
def add_formatter(formatter_type, output_path = nil)
  formatter_class = case formatter_type
                    when Class
                      formatter_type
                    when /\A[A-Z]/
                      custom_formatter_class(formatter_type)
                    else
                      builtin_formatter_class(formatter_type)
                    end

  if output_path
    dir_path = File.dirname(output_path)
    FileUtils.mkdir_p(dir_path) unless File.exist?(dir_path)
    output = File.open(output_path, 'w')
  else
    output = $stdout
  end

  self << formatter_class.new(output, @options)
end
close_output_files() click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 69
def close_output_files
  each do |formatter|
    formatter.output.close if formatter.output.is_a?(File)
  end
end
file_finished(file, offenses) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 43
def file_finished(file, offenses)
  each { |f| f.file_finished(file, offenses) }
  offenses
end
file_started(file, options) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 37
def file_started(file, options)
  @options = options[:cli_options]
  @config_store = options[:config_store]
  each { |f| f.file_started(file, options) }
end

Private Instance Methods

builtin_formatter_class(specified_key) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 77
def builtin_formatter_class(specified_key)
  matching_keys = BUILTIN_FORMATTERS_FOR_KEYS.keys.select do |key|
    key.start_with?(specified_key)
  end

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

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

  BUILTIN_FORMATTERS_FOR_KEYS[matching_keys.first]
end
custom_formatter_class(specified_class_name) click to toggle source
# File lib/rubocop/formatter/formatter_set.rb, line 91
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