class RubbyCop::Cop::Registry
Registry
that tracks all cops by their badge and department.
Public Class Methods
new(cops = [])
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 25 def initialize(cops = []) @registry = {} @departments = {} cops.each { |cop| enlist(cop) } end
Public Instance Methods
==(other)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 127 def ==(other) cops == other.cops end
contains_cop_matching?(names)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 56 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end
cops()
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 109 def cops @registry.values end
departments()
click to toggle source
@return [Array<Symbol>] list of departments for current cops.
# File lib/rubbycop/cop/registry.rb, line 39 def departments @departments.keys end
each(&block)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 141 def each(&block) cops.each(&block) end
enabled(config, only)
click to toggle source
# File lib/rubbycop/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)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 32 def enlist(cop) @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop end
length()
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 113 def length @registry.size end
names()
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 123 def names cops.map(&:cop_name) end
qualified_cop_name(name, path)
click to toggle source
Convert a user provided cop name into a properly namespaced name
@example gives back a correctly qualified cop name
cops = RubbyCop::Cop::Cop.all cops. qualified_cop_name('Layout/IndentArray') # => 'Layout/IndentArray'
@example fixes incorrect namespaces
cops = RubbyCop::Cop::Cop.all cops.qualified_cop_name('Lint/IndentArray') # => 'Layout/IndentArray'
@example namespaces bare cop identifiers
cops = RubbyCop::Cop::Cop.all cops.qualified_cop_name('IndentArray') # => 'Layout/IndentArray'
@example passes back unrecognized cop names
cops = RubbyCop::Cop::Cop.all cops.qualified_cop_name('NotACop') # => 'NotACop'
@param name [String] Cop
name extracted from config @param path [String, nil] Path of file that `name` was extracted from
@raise [AmbiguousCopName]
if a bare identifier with two possible namespaces is provided
@note Emits a warning if the provided name has an incorrect namespace
@return [String] Qualified cop name
# File lib/rubbycop/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)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 137 def select(&block) cops.select(&block) end
sort!()
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 131 def sort! @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }] self end
to_h()
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 105 def to_h cops.group_by(&:cop_name) end
with_department(department)
click to toggle source
@return [Registry] Cops for that specific department.
# File lib/rubbycop/cop/registry.rb, line 44 def with_department(department) with(@departments.fetch(department, [])) end
without_department(department)
click to toggle source
@return [Registry] Cops not for a specific department.
# File lib/rubbycop/cop/registry.rb, line 49 def without_department(department) without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end
Private Instance Methods
qualify_badge(badge)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 151 def qualify_badge(badge) @departments .map { |department, _| badge.with_department(department) } .select { |potential_badge| registered?(potential_badge) } end
registered?(badge)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 166 def registered?(badge) @registry.key?(badge) end
resolve_badge(given_badge, real_badge, source_path)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 157 def resolve_badge(given_badge, real_badge, source_path) unless given_badge.match?(real_badge) warn "#{source_path}: #{given_badge} has the wrong namespace - " \ "should be #{real_badge.department}" end real_badge.to_s end
with(cops)
click to toggle source
# File lib/rubbycop/cop/registry.rb, line 147 def with(cops) self.class.new(cops) end