lib/rubocop/cop/registry.rb in rubocop-0.57.2 vs lib/rubocop/cop/registry.rb in rubocop-0.58.0

- old
+ new

@@ -21,20 +21,22 @@ end # Registry that tracks all cops by their badge and department. class Registry def initialize(cops = []) - @registry = {} + @registry = {} @departments = {} + @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] } cops.each { |cop| enlist(cop) } end def enlist(cop) @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop + @cops_by_cop_name[cop.cop_name] << cop end # @return [Array<Symbol>] list of departments for current cops. def departments @departments.keys @@ -100,12 +102,13 @@ when 1 then resolve_badge(badge, potential_badges.first, path) else raise AmbiguousCopName.new(badge, path, potential_badges) end end + # @return [Hash{String => Array<Class>}] def to_h - cops.group_by(&:cop_name) + @cops_by_cop_name end def cops @registry.values end @@ -138,9 +141,15 @@ cops.select(&block) end def each(&block) cops.each(&block) + end + + # @param [String] cop_name + # @return [Class, nil] + def find_by_cop_name(cop_name) + @cops_by_cop_name[cop_name].first end private def with(cops)