class RailsBestPractices::Lexicals::LongLineCheck

Keep lines fewer than 80 characters.

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method RailsBestPractices::Core::Check::new
# File lib/rails_best_practices/lexicals/long_line_check.rb, line 9
def initialize(options = {})
  super(options)
  @max_line_length = options['max_line_length'] || 80
end

Public Instance Methods

check(filename, content) click to toggle source

check if a line is over 80 characters

@param [String] filename name of the file @param [String] content content of the file

# File lib/rails_best_practices/lexicals/long_line_check.rb, line 18
def check(filename, content)
  # Only check ruby files
  if /\.rb$/ =~ filename
    line_no = 0
    content.each_line do |line|
      line_no += 1
      actual_line_length = line.sub(/\s+$/, '').length
      next unless actual_line_length > @max_line_length

      add_error(
        "line is longer than #{@max_line_length} characters (#{actual_line_length} characters)",
        filename,
        line_no
      )
    end
  end
end