Class: Pronto::Credo::OutputParser

Inherits:
Object
  • Object
show all
Defined in:
lib/pronto/credo/output_parser.rb

Constant Summary collapse

TYPE_WARNINGS =
{ 'R' => :info,
  'W' => :warning,
  'C' => :info,
  'D' => :info,
  'F' => :info,
  'A' => :info
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, output) ⇒ OutputParser

Returns a new instance of OutputParser.



13
14
15
16
# File 'lib/pronto/credo/output_parser.rb', line 13

def initialize(file, output)
  @file = file
  @output = output
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/pronto/credo/output_parser.rb', line 4

def file
  @file
end

#outputObject (readonly)

Returns the value of attribute output.



4
5
6
# File 'lib/pronto/credo/output_parser.rb', line 4

def output
  @output
end

Instance Method Details

#parseObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pronto/credo/output_parser.rb', line 18

def parse
  output.lines.map do |line|
    line_parts = line.split(':')
    next unless file.start_with?(line_parts[0])
    offence_in_line = line_parts[1]
    column_line = nil
    if line_parts[2].to_i == 0
      offence_level = TYPE_WARNINGS[line_parts[2].strip]
      offence_message = line_parts[3..-1].join(':').strip
    else
      offence_level = TYPE_WARNINGS[line_parts[3].strip]
      column_line = line_parts[2].to_i
      offence_message = line_parts[4..-1].join(':').strip
    end
    {
      line: offence_in_line.to_i,
      column: column_line,
      level: offence_level,
      message: offence_message
    }
  end.compact
end