Class: Stormbreaker::ErbFormatter

Inherits:
Object
  • Object
show all
Includes:
ERB::Util
Defined in:
lib/stormbreaker/erb_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(violations) ⇒ ErbFormatter

Returns a new instance of ErbFormatter.



10
11
12
13
# File 'lib/stormbreaker/erb_formatter.rb', line 10

def initialize(violations)
  @violations = sort_violations(violations)
  @severity_categories = Stormbreaker.configuration.enabled_severity_categories
end

Instance Attribute Details

#violationsObject (readonly)

Returns the value of attribute violations.



8
9
10
# File 'lib/stormbreaker/erb_formatter.rb', line 8

def violations
  @violations
end

Instance Method Details

#parse_callstack(violation) ⇒ Object



33
34
35
36
37
# File 'lib/stormbreaker/erb_formatter.rb', line 33

def parse_callstack(violation)
  return if violation.called_by.to_a.empty?

  violation.called_by.to_a.map { |spec| "  #{spec}" }.join("\n")
end

#sort_violations(violations) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/stormbreaker/erb_formatter.rb', line 15

def sort_violations(violations)
  sorted = {}
  Stormbreaker.configuration.enabled_severity_categories.each do |severity|
    sorted[severity] = violations.select { |v| v.severity == severity }
  end
  sorted
end

#templateObject



39
40
41
# File 'lib/stormbreaker/erb_formatter.rb', line 39

def template
  File.read(File.join(File.dirname(__FILE__), "../templates/results.erb"))
end

#total_violationsObject



29
30
31
# File 'lib/stormbreaker/erb_formatter.rb', line 29

def total_violations
  @violations.reduce(0) { |key, (_category, violations)| key + violations.count }
end

#write_to_erbObject



23
24
25
26
27
# File 'lib/stormbreaker/erb_formatter.rb', line 23

def write_to_erb
  erb = ERB.new(template)
  File.write(Stormbreaker.configuration.results_path, erb.result(binding))
  puts "Results written to #{Stormbreaker.configuration.results_path}"
end