Class: Obcd::Checks::HeaderStyle

Inherits:
Check
  • Object
show all
Defined in:
lib/obcd/checks/header_style.rb

Instance Attribute Summary

Attributes inherited from Check

#filename, #violations

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Check

check, #initialize

Constructor Details

This class inherits a constructor from Obcd::Checks::Check

Class Method Details

.descObject



4
5
6
# File 'lib/obcd/checks/header_style.rb', line 4

def self.desc
  'Normalize header style on top of .m and .h files.'
end

Instance Method Details

#check!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/obcd/checks/header_style.rb', line 8

def check!
  if head.select { |line| line.start_with?('//') }.count != 7
    violations << Obcd::Violations::Violation.new(1, "Number of lines in header doesn't match.")
  elsif head.select { |line| (line.start_with?('//') || line.start_with?('//  ')) && !line.start_with?('//   ') }.count != 7
    violations << Obcd::Violations::Violation.new(1, "Header text alignment or number of spaces in header doesn't match.")
  elsif head[7].strip != ''
    violations << Obcd::Violations::Violation.new(1, 'Missing empty line after header.')
  else
    # opening
    violations << Obcd::Violations::Violation.new(1, 'Expected header to start with a //.') unless head[0] == '//'
    # filename
    if head[1] != "//  #{File.basename(filename)}"
      if head[1].downcase == "//  #{File.basename(filename).downcase}"
        violations << Obcd::Violations::Violation.new(2, "Filename case doesn't match, expected #{File.basename(filename)}, got #{head[1]}.")
      elsif (extension = File.extname(head[1].split('//').last)) != File.extname(filename)
        violations << Obcd::Violations::Violation.new(2, "Filename extension doesn't match, expected #{File.extname(filename)}, got #{extension}.")
      else
        violations << Obcd::Violations::Violation.new(2, "Expected header to include file name, instead got #{head[1]}.") unless head[1] == "//  #{File.basename(filename)}"
      end
    end
    # TODO: check company
    # blank line
    violations << Obcd::Violations::Violation.new(4, 'Expected a blank // after company name.') unless head[3] == '//'
    # TODO: created by
    # TODO: copyright
    violations << Obcd::Violations::Violation.new(7, 'Expected a blank // after copyright.') unless head[3] == '//'
  end
end