Class: RuboCop::Cop::Badge
- Inherits:
-
Object
- Object
- RuboCop::Cop::Badge
- 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::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.
Instance Attribute Summary collapse
-
#cop_name ⇒ Object
readonly
Returns the value of attribute cop_name.
-
#department ⇒ Object
readonly
Returns the value of attribute department.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(class_name_parts) ⇒ Badge
constructor
A new instance of Badge.
- #match?(other) ⇒ Boolean
- #qualified? ⇒ Boolean
- #to_s ⇒ Object
- #with_department(department) ⇒ Object
Constructor Details
#initialize(class_name_parts) ⇒ Badge
Returns a new instance of Badge.
25 26 27 28 29 |
# File 'lib/rubocop/cop/badge.rb', line 25 def initialize(class_name_parts) department_parts = class_name_parts[0...-1] @department = (department_parts.join('/').to_sym unless department_parts.empty?) @cop_name = class_name_parts.last end |
Instance Attribute Details
#cop_name ⇒ Object (readonly)
Returns the value of attribute cop_name
13 14 15 |
# File 'lib/rubocop/cop/badge.rb', line 13 def cop_name @cop_name end |
#department ⇒ Object (readonly)
Returns the value of attribute department
13 14 15 |
# File 'lib/rubocop/cop/badge.rb', line 13 def department @department end |
Class Method Details
.for(class_name) ⇒ Object
15 16 17 18 19 |
# 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..-1] : parts.last(2)) end |
.parse(identifier) ⇒ Object
21 22 23 |
# File 'lib/rubocop/cop/badge.rb', line 21 def self.parse(identifier) new(identifier.split('/')) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
31 32 33 |
# File 'lib/rubocop/cop/badge.rb', line 31 def ==(other) hash == other.hash end |
#hash ⇒ Object
36 37 38 |
# File 'lib/rubocop/cop/badge.rb', line 36 def hash [department, cop_name].hash end |
#match?(other) ⇒ Boolean
40 41 42 43 |
# File 'lib/rubocop/cop/badge.rb', line 40 def match?(other) cop_name == other.cop_name && (!qualified? || department == other.department) end |
#qualified? ⇒ Boolean
49 50 51 |
# File 'lib/rubocop/cop/badge.rb', line 49 def qualified? !department.nil? end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/rubocop/cop/badge.rb', line 45 def to_s @to_s ||= qualified? ? "#{department}/#{cop_name}" : cop_name end |
#with_department(department) ⇒ Object
53 54 55 |
# File 'lib/rubocop/cop/badge.rb', line 53 def with_department(department) self.class.new([department.to_s.split('/'), cop_name].flatten) end |