Class: RuboCop::Cop::Badge

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

Overview

Identifier of all cops containing a department and cop name.

All cops are identified by their badge. For example, the badge for ‘RuboCop::Cop::Style::Tab` is `Style/Tab`. Badges can be parsed as either `Department/CopName` or just `CopName` to allow for badge references in source files that omit the department for RuboCop to infer.

Defined Under Namespace

Classes: InvalidBadge

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(department, cop_name) ⇒ Badge

Returns a new instance of Badge.



41
42
43
44
# File 'lib/rubocop/cop/badge.rb', line 41

def initialize(department, cop_name)
  @department = department.to_sym if department
  @cop_name   = cop_name
end

Instance Attribute Details

#cop_nameObject (readonly)

Returns the value of attribute cop_name.



23
24
25
# File 'lib/rubocop/cop/badge.rb', line 23

def cop_name
  @cop_name
end

#departmentObject (readonly)

Returns the value of attribute department.



23
24
25
# File 'lib/rubocop/cop/badge.rb', line 23

def department
  @department
end

Class Method Details

.for(class_name) ⇒ Object



25
26
27
# File 'lib/rubocop/cop/badge.rb', line 25

def self.for(class_name)
  new(*class_name.split('::').last(2))
end

.parse(identifier) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/badge.rb', line 29

def self.parse(identifier)
  parts = identifier.split('/', 2)

  raise InvalidBadge, identifier if parts.size > 2

  if parts.one?
    new(nil, *parts)
  else
    new(*parts)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



46
47
48
# File 'lib/rubocop/cop/badge.rb', line 46

def ==(other)
  hash == other.hash
end

#hashObject



51
52
53
# File 'lib/rubocop/cop/badge.rb', line 51

def hash
  [department, cop_name].hash
end

#match?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/rubocop/cop/badge.rb', line 55

def match?(other)
  cop_name == other.cop_name &&
    (!qualified? || department == other.department)
end

#qualified?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/rubocop/cop/badge.rb', line 64

def qualified?
  !department.nil?
end

#to_sObject



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

def to_s
  qualified? ? "#{department}/#{cop_name}" : cop_name
end

#with_department(department) ⇒ Object



68
69
70
# File 'lib/rubocop/cop/badge.rb', line 68

def with_department(department)
  self.class.new(department, cop_name)
end