Class: RubocopChallenger::Rubocop::Rule

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubocop_challenger/rubocop/rule.rb

Overview

Parse rubocop rule which loaded from Rubocop::TodoReader class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ Rule

Returns a new instance of Rule.



11
12
13
14
15
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 11

def initialize(contents)
  @contents = contents.dup
  @title = extract_title
  @offense_count = extract_offense_count
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



9
10
11
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 9

def contents
  @contents
end

#offense_countObject (readonly)

Returns the value of attribute offense_count.



9
10
11
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 9

def offense_count
  @offense_count
end

#titleObject (readonly)

Returns the value of attribute title.



9
10
11
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 9

def title
  @title
end

Instance Method Details

#<=>(other) ⇒ Object



25
26
27
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 25

def <=>(other)
  offense_count <=> other.offense_count
end

#==(other) ⇒ Boolean

Compare to other rule

Parameters:

  • other (Rule)

    Target of comparison

Returns:

  • (Boolean)

    Return true in case of coincidence



21
22
23
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 21

def ==(other)
  title == other.title
end

#autocorrectable?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 29

def autocorrectable?
  contents.match?('# This cop supports (un)?safe autocorrection') || # for rubocop >= v1.30.0
    contents.match?(/# This cop supports (un)?safe auto-correction/) || # for rubocop >= v1.26.0
    contents.include?('# Cop supports --auto-correct') # for rubocop < v1.26.0
end

#rubydoc_urlObject



35
36
37
38
39
40
41
# File 'lib/rubocop_challenger/rubocop/rule.rb', line 35

def rubydoc_url
  if title.start_with?('RSpec')
    "https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/#{title}"
  else
    "https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/#{title}"
  end
end