123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Badge

Relationships & Source Files
Inherits: Object
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 Layout::IndentationStyle is Layout/IndentationStyle. 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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(class_name_parts) ⇒ Badge

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 34

def initialize(class_name_parts)
  department_parts = class_name_parts[0...-1]
  @department = (department_parts.join('/').to_sym unless department_parts.empty?)
  @department_name = @department&.to_s
  @cop_name = class_name_parts.last
end

Class Method Details

.camel_case(name_part)

[ GitHub ]

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

def self.camel_case(name_part)
  return 'RSpec' if name_part == 'rspec'
  return name_part unless name_part.match?(/^[a-z]|_[a-z]/)

  name_part.gsub(/^[a-z]|_[a-z]/) { |match| match[-1, 1].upcase }
end

.for(class_name)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 15

def self.for(class_name)
  parts = class_name.split('::')
  name_deep_enough = parts.length >= 4
  new(name_deep_enough ? parts[2..] : parts.last(2))
end

.parse(identifier)

[ GitHub ]

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

def self.parse(identifier)
  @parse_cache[identifier] ||= new(identifier.split('/').map! { |i| camel_case(i) })
end

Instance Attribute Details

#cop_name (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 13

attr_reader :department, :department_name, :cop_name

#department (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 13

attr_reader :department, :department_name, :cop_name

#department_name (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 13

attr_reader :department, :department_name, :cop_name

#qualified?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 59

def qualified?
  !department.nil?
end

Instance Method Details

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

[ GitHub ]

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

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

#eql?(other)

Alias for #==.

[ GitHub ]

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

alias eql? ==

#hash

[ GitHub ]

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

def hash
  # Do hashing manually to reduce Array allocations.
  department.hash ^ cop_name.hash # rubocop:disable Security/CompoundHash
end

#match?(other) ⇒ Boolean

[ GitHub ]

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

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

#to_s

[ GitHub ]

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

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

#with_department(department)

[ GitHub ]

  
# File 'lib/rubocop/cop/badge.rb', line 63

def with_department(department)
  self.class.new([department.to_s.split('/'), cop_name].flatten)
end