Class: RuboCop::Formatter::HTMLFormatter

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

Overview

This formatter saves the output as a html file.

Defined Under Namespace

Classes: Color, ERBContext

Constant Summary collapse

TEMPLATE_PATH =
File.expand_path('../../../../assets/output.html.erb', __FILE__)

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output) ⇒ HTMLFormatter

Returns a new instance of HTMLFormatter.


18
19
20
21
22
# File 'lib/rubocop/formatter/html_formatter.rb', line 18

def initialize(output)
  super
  @files = []
  @summary = OpenStruct.new(offense_count: 0)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files


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

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary


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

def summary
  @summary
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object


28
29
30
31
# File 'lib/rubocop/formatter/html_formatter.rb', line 28

def file_finished(file, offenses)
  files << OpenStruct.new(path: file, offenses: offenses)
  summary.offense_count += offenses.count
end

#finished(inspected_files) ⇒ Object


33
34
35
36
37
# File 'lib/rubocop/formatter/html_formatter.rb', line 33

def finished(inspected_files)
  summary.inspected_files = inspected_files

  render_html
end

#render_htmlObject


39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/formatter/html_formatter.rb', line 39

def render_html
  context = ERBContext.new(files, summary)

  template = File.read(TEMPLATE_PATH, encoding: Encoding::UTF_8)
  erb = ERB.new(template, nil, '-')
  html = erb.result(context.binding)

  output.write html
end

#started(target_files) ⇒ Object


24
25
26
# File 'lib/rubocop/formatter/html_formatter.rb', line 24

def started(target_files)
  summary.target_files = target_files
end