Class: Cane::DocCheck

Inherits:
Struct
  • Object
show all
Defined in:
lib/cane/doc_check.rb

Overview

Creates violations for class definitions that do not have an explantory comment immediately preceeding.

Constant Summary collapse

MAGIC_COMMENT_REGEX =

Stolen from ERB source.

%r"coding\s*[=:]\s*([[:alnum:]\-_]+)"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



7
8
9
# File 'lib/cane/doc_check.rb', line 7

def opts
  @opts
end

Class Method Details

.keyObject



9
# File 'lib/cane/doc_check.rb', line 9

def self.key; :doc; end

.nameObject



10
# File 'lib/cane/doc_check.rb', line 10

def self.name; "documentation checking"; end

.optionsObject



11
12
13
14
15
16
17
18
19
# File 'lib/cane/doc_check.rb', line 11

def self.options
  {
    doc_glob: ['Glob to run doc checks over',
                  default:  '{app,lib}/**/*.rb',
                  variable: 'GLOB',
                  clobber:  :no_doc],
    no_doc:   ['Disable documentation checking', cast: ->(x) { !x }]
  }
end

Instance Method Details

#class_definition?(line) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cane/doc_check.rb', line 52

def class_definition?(line)
  line =~ /^\s*class\s+/ and $'.index('<<') != 0
end

#comment?(line) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cane/doc_check.rb', line 56

def comment?(line)
  line =~ /^\s*#/ && !(MAGIC_COMMENT_REGEX =~ line)
end

#extract_class_name(line) ⇒ Object



60
61
62
# File 'lib/cane/doc_check.rb', line 60

def extract_class_name(line)
  line.match(/class ([^\s;]+)/)[1]
end

#file_namesObject



48
49
50
# File 'lib/cane/doc_check.rb', line 48

def file_names
  Dir[opts.fetch(:doc_glob)]
end

#find_violations(file_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cane/doc_check.rb', line 32

def find_violations(file_name)
  last_line = ""
  Cane::File.iterator(file_name).map.with_index do |line, number|
    result = if class_definition?(line) && !comment?(last_line)
      {
        file:        file_name,
        line:        number + 1,
        label:       extract_class_name(line),
        description: "Classes are not documented"
      }
    end
    last_line = line
    result
  end.compact
end

#violationsObject



24
25
26
27
28
29
30
# File 'lib/cane/doc_check.rb', line 24

def violations
  return [] if opts[:no_doc]

  worker.map(file_names) {|file_name|
    find_violations(file_name)
  }.flatten
end

#workerObject



64
65
66
# File 'lib/cane/doc_check.rb', line 64

def worker
  Cane.task_runner(opts)
end