Class: RuboCop::Cop::Registry
- Inherits:
-
Object
- Object
- RuboCop::Cop::Registry
- Defined in:
- lib/rubocop/cop/registry.rb
Overview
Registry that tracks all cops by their badge and department.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #contains_cop_matching?(names) ⇒ Boolean
- #cops ⇒ Object
-
#departments ⇒ Array<Symbol>
List of departments for current cops.
- #each(&block) ⇒ Object
- #enabled(config, only) ⇒ Object
- #enlist(cop) ⇒ Object
-
#initialize(cops = []) ⇒ Registry
constructor
A new instance of Registry.
- #length ⇒ Object
- #names ⇒ Object
-
#qualified_cop_name(name, path) ⇒ String
Convert a user provided cop name into a properly namespaced name.
- #select(&block) ⇒ Object
- #sort! ⇒ Object
- #to_h ⇒ Object
-
#with_department(department) ⇒ Registry
Cops for that specific department.
-
#without_department(department) ⇒ Registry
Cops not for a specific department.
Constructor Details
#initialize(cops = []) ⇒ Registry
Returns a new instance of Registry.
25 26 27 28 29 30 |
# File 'lib/rubocop/cop/registry.rb', line 25 def initialize(cops = []) @registry = {} @departments = {} cops.each { |cop| enlist(cop) } end |
Instance Method Details
#==(other) ⇒ Object
127 128 129 |
# File 'lib/rubocop/cop/registry.rb', line 127 def ==(other) cops == other.cops end |
#contains_cop_matching?(names) ⇒ Boolean
56 57 58 |
# File 'lib/rubocop/cop/registry.rb', line 56 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end |
#cops ⇒ Object
109 110 111 |
# File 'lib/rubocop/cop/registry.rb', line 109 def cops @registry.values end |
#departments ⇒ Array<Symbol>
Returns list of departments for current cops.
39 40 41 |
# File 'lib/rubocop/cop/registry.rb', line 39 def departments @departments.keys end |
#each(&block) ⇒ Object
141 142 143 |
# File 'lib/rubocop/cop/registry.rb', line 141 def each(&block) cops.each(&block) end |
#enabled(config, only) ⇒ Object
117 118 119 120 121 |
# File 'lib/rubocop/cop/registry.rb', line 117 def enabled(config, only) select do |cop| config.for_cop(cop).fetch('Enabled') || only.include?(cop.cop_name) end end |
#enlist(cop) ⇒ Object
32 33 34 35 36 |
# File 'lib/rubocop/cop/registry.rb', line 32 def enlist(cop) @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop end |
#length ⇒ Object
113 114 115 |
# File 'lib/rubocop/cop/registry.rb', line 113 def length @registry.size end |
#names ⇒ Object
123 124 125 |
# File 'lib/rubocop/cop/registry.rb', line 123 def names cops.map(&:cop_name) end |
#qualified_cop_name(name, path) ⇒ String
Note:
Emits a warning if the provided name has an incorrect namespace
Convert a user provided cop name into a properly namespaced name
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rubocop/cop/registry.rb', line 92 def qualified_cop_name(name, path) badge = Badge.parse(name) return name if registered?(badge) potential_badges = qualify_badge(badge) case potential_badges.size when 0 then name # No namespace found. Deal with it later in caller. when 1 then resolve_badge(badge, potential_badges.first, path) else raise AmbiguousCopName.new(badge, path, potential_badges) end end |
#select(&block) ⇒ Object
137 138 139 |
# File 'lib/rubocop/cop/registry.rb', line 137 def select(&block) cops.select(&block) end |
#sort! ⇒ Object
131 132 133 134 135 |
# File 'lib/rubocop/cop/registry.rb', line 131 def sort! @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }] self end |
#to_h ⇒ Object
105 106 107 |
# File 'lib/rubocop/cop/registry.rb', line 105 def to_h cops.group_by(&:cop_name) end |
#with_department(department) ⇒ Registry
Returns Cops for that specific department.
44 45 46 |
# File 'lib/rubocop/cop/registry.rb', line 44 def with_department(department) with(@departments.fetch(department, [])) end |
#without_department(department) ⇒ Registry
Returns Cops not for a specific department.
49 50 51 52 53 54 |
# File 'lib/rubocop/cop/registry.rb', line 49 def without_department(department) without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end |