Class: Splam::Rules::LineLength

Inherits:
Splam::Rule show all
Defined in:
lib/splam/rules/line_length.rb

Instance Attribute Summary

Attributes inherited from Splam::Rule

#body, #reasons, #score, #suite, #weight

Instance Method Summary collapse

Methods inherited from Splam::Rule

#add_score, inherited, #initialize, #line_safe?, run

Constructor Details

This class inherits a constructor from Splam::Rule

Instance Method Details

#nameObject



3
4
5
# File 'lib/splam/rules/line_length.rb', line 3

def name
  "Line length"
end

#runObject

Penalize long line lengths.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/splam/rules/line_length.rb', line 8

def run
  lines = @body.split("\n")
  lines.each do |line|
    next if line =~ /\A\s{4,}/ # ignore code blocks

    # multiplier = (lines.size == 1) ? 10 : 1 # one line? fail.
    multiplier = 1
    
    # 1 point for each 40 chars in a line.
    hits = (line.size / 40) * multiplier
    add_score hits, "lines over 40 chars"
    
    # 2 more points if line is longer than 80
    hits = (line.size / 80) * 2 * multiplier
    add_score hits, "lines over 80 chars"

  end
end