Class: Tuxedo::CaneParser

Inherits:
Object
  • Object
show all
Defined in:
lib/tuxedo/cane_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCaneParser

Returns a new instance of CaneParser.



5
6
7
# File 'lib/tuxedo/cane_parser.rb', line 5

def initialize
  self.result = { }
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



3
4
5
# File 'lib/tuxedo/cane_parser.rb', line 3

def result
  @result
end

Instance Method Details

#get_error(line) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/tuxedo/cane_parser.rb', line 37

def get_error(line)
  if line.include?("Line is >80 characters")
    error = "Line is >80 characters"
  elsif line.include?("Line contains trailing whitespaces")
    error = "Line contains trailing whitespaces"
  elsif line.include?(">") # abc complexity errors have a > char too
    error = "Maximum allowed ABC complexity"
  end
  error
end

#get_file(line) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/tuxedo/cane_parser.rb', line 48

def get_file(line)
  if line.include?(":")
    line.match(/([a-zA-Z0-9.\/_]+):/)[1]
  else
    line.split(" ").first
  end
end

#get_lines(line) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/tuxedo/cane_parser.rb', line 56

def get_lines(line)
  if line.include?(":")
    [ line.match(/:(\d+)/)[1].to_i ]
  else
    [ line.split(" ").last.to_i ]
  end
end

#has_error?(line) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/tuxedo/cane_parser.rb', line 33

def has_error?(line)
  line.include?(">") || line.include?("Line contains trailing whitespaces")
end

#parse_cane(output) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/tuxedo/cane_parser.rb', line 9

def parse_cane(output)
  output.split("\n").each do |line|
    if has_error?(line)
      error = process_line(line)
      push_error(error)
    end
  end
  return self
end

#process_line(line) ⇒ Object



27
28
29
30
31
# File 'lib/tuxedo/cane_parser.rb', line 27

def process_line(line)
  Tuxedo::Error.new( :name => get_error(line),
                         :source => get_file(line),
                         :line => get_lines(line))
end

#push_error(error) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/tuxedo/cane_parser.rb', line 19

def push_error(error)
  if self.result[ error.name.to_sym ]
    self.result[ error.name.to_sym ] << error
  else
    self.result[ error.name.to_sym ] = [ error ]
  end
end