Class: Rubomop::Cop

Inherits:
Object
  • Object
show all
Defined in:
lib/rubomop/cop.rb

Constant Summary collapse

OFFENSE_COUNT_REGEX =
/\A# Offense count: (\d*)/
COP_NAME_REGEX =
/\A(.*):/
FILE_NAME_REGEX =
/- '(.*)'/
AUTOCORRECT_REGEX =
/\A# Cop supports --auto-correct./
GENERAL_COMMENT_REGEX =
/\A#/
EXCLUDE_REGEX =
/Exclude:/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_lines) ⇒ Cop

Returns a new instance of Cop.



12
13
14
15
16
17
# File 'lib/rubomop/cop.rb', line 12

def initialize(raw_lines)
  @raw_lines = raw_lines
  @files = []
  @autocorrect = false
  @comments = []
end

Instance Attribute Details

#autocorrectObject

Returns the value of attribute autocorrect.



3
4
5
# File 'lib/rubomop/cop.rb', line 3

def autocorrect
  @autocorrect
end

#commentsObject

Returns the value of attribute comments.



3
4
5
# File 'lib/rubomop/cop.rb', line 3

def comments
  @comments
end

#filesObject

Returns the value of attribute files.



3
4
5
# File 'lib/rubomop/cop.rb', line 3

def files
  @files
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/rubomop/cop.rb', line 3

def name
  @name
end

#offense_countObject

Returns the value of attribute offense_count.



3
4
5
# File 'lib/rubomop/cop.rb', line 3

def offense_count
  @offense_count
end

#raw_linesObject (readonly)

Returns the value of attribute raw_lines.



4
5
6
# File 'lib/rubomop/cop.rb', line 4

def raw_lines
  @raw_lines
end

Class Method Details

.create_and_parse(raw_lines) ⇒ Object



6
7
8
9
10
# File 'lib/rubomop/cop.rb', line 6

def self.create_and_parse(raw_lines)
  result = new(raw_lines)
  result.parse
  result
end

Instance Method Details

#delete!(filename) ⇒ Object



64
65
66
67
# File 'lib/rubomop/cop.rb', line 64

def delete!(filename)
  files.delete(filename)
  self.offense_count -= 1
end

#delete_optionsObject



60
61
62
# File 'lib/rubomop/cop.rb', line 60

def delete_options
  files.map { {cop: self, file: _1} }
end

#outputObject



47
48
49
# File 'lib/rubomop/cop.rb', line 47

def output
  output_lines.join("\n")
end

#output_linesObject



51
52
53
54
55
56
57
58
# File 'lib/rubomop/cop.rb', line 51

def output_lines
  result = ["# Offense count: #{offense_count}"]
  result << "# Cop supports --auto-correct." if autocorrect
  result += comments
  result << "#{name}:"
  result << "  Exclude:"
  result + files.map { "    - '#{_1}'"}
end

#parseObject



19
20
21
# File 'lib/rubomop/cop.rb', line 19

def parse
  raw_lines.each { parse_one_line(_1) }
end

#parse_one_line(line) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubomop/cop.rb', line 30

def parse_one_line(line)
  case line
  when OFFENSE_COUNT_REGEX
    self.offense_count = line.match(OFFENSE_COUNT_REGEX)[1].to_i
  when AUTOCORRECT_REGEX
    self.autocorrect = true
  when GENERAL_COMMENT_REGEX
    comments << line.chomp
  when EXCLUDE_REGEX
    # no-op
  when COP_NAME_REGEX
    self.name = line.match(COP_NAME_REGEX)[1]
  when FILE_NAME_REGEX
    files << line.match(FILE_NAME_REGEX)[1]
  end
end