Class: FitCommit::Validators::LineLength

Inherits:
Base
  • Object
show all
Defined in:
lib/fit-commit/validators/line_length.rb

Constant Summary collapse

FIRST_LINE_MAX_LENGTH =
50
LINE_MAX_LENGTH =
72

Instance Attribute Summary

Attributes inherited from Base

#branch_name, #lines

Instance Method Summary collapse

Methods inherited from Base

inherited, #initialize, #validate

Methods included from HasErrors

#add_error, #add_warning, #errors, #merge_errors, #merge_warnings, #warnings

Constructor Details

This class inherits a constructor from FitCommit::Validators::Base

Instance Method Details

#contains_url?(text) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fit-commit/validators/line_length.rb', line 27

def contains_url?(text)
  text =~ %r{[a-z]+://}
end

#line_too_long?(text) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fit-commit/validators/line_length.rb', line 23

def line_too_long?(text)
  text.length > 72 && !contains_url?(text)
end

#validate_line(lineno, text, _branch_name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fit-commit/validators/line_length.rb', line 9

def validate_line(lineno, text, _branch_name)
  if lineno == 1 && text.empty?
    add_error(lineno, "First line cannot be blank.")
  elsif lineno == 2 && !text.empty?
    add_error(lineno, "Second line must be blank.")
  elsif line_too_long?(text)
    add_error(lineno, format("Lines should be <= %i chars. (%i)",
      LINE_MAX_LENGTH, text.length))
  elsif lineno == 1 && text.length > FIRST_LINE_MAX_LENGTH
    add_warning(lineno, format("First line should be <= %i chars. (%i)",
      FIRST_LINE_MAX_LENGTH, text.length))
  end
end