Class: ThemeCheck::JsonPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/json_printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(out_stream = STDOUT) ⇒ JsonPrinter

Returns a new instance of JsonPrinter.



6
7
8
# File 'lib/theme_check/json_printer.rb', line 6

def initialize(out_stream = STDOUT)
  @out = out_stream
end

Instance Method Details

#offenses_by_path(offenses) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/theme_check/json_printer.rb', line 15

def offenses_by_path(offenses)
  offenses
    .map(&:to_h)
    .group_by { |offense| offense[:path] }
    .map do |(path, path_offenses)|
      {
        path: path,
        offenses: path_offenses.map { |offense| offense.filter { |k, _v| k != :path } },
        errorCount: path_offenses.count { |offense| offense[:severity] == Check::SEVERITY_VALUES[:error] },
        suggestionCount: path_offenses.count { |offense| offense[:severity] == Check::SEVERITY_VALUES[:suggestion] },
        styleCount: path_offenses.count { |offense| offense[:severity] == Check::SEVERITY_VALUES[:style] },
      }
    end
    .sort_by { |o| o[:path] || Pathname.new('') }
end


10
11
12
13
# File 'lib/theme_check/json_printer.rb', line 10

def print(offenses)
  json = offenses_by_path(offenses)
  @out.puts JSON.dump(json)
end