class RuboCop::Cop::Registry

Registry that tracks all cops by their badge and department.

Public Class Methods

new(cops = []) click to toggle source
# File lib/rubocop/cop/registry.rb, line 24
def initialize(cops = [])
  @registry    = {}
  @departments = {}

  cops.each { |cop| enlist(cop) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/rubocop/cop/registry.rb, line 126
def ==(other)
  cops == other.cops
end
contains_cop_matching?(names) click to toggle source
# File lib/rubocop/cop/registry.rb, line 55
def contains_cop_matching?(names)
  cops.any? { |cop| cop.match?(names) }
end
cops() click to toggle source
# File lib/rubocop/cop/registry.rb, line 108
def cops
  @registry.values
end
departments() click to toggle source

@return [Array<Symbol>] list of departments for current cops.

# File lib/rubocop/cop/registry.rb, line 38
def departments
  @departments.keys
end
each(&block) click to toggle source
# File lib/rubocop/cop/registry.rb, line 140
def each(&block)
  cops.each(&block)
end
enabled(config, only) click to toggle source
# File lib/rubocop/cop/registry.rb, line 116
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/rubocop/cop/registry.rb, line 31
def enlist(cop)
  @registry[cop.badge] = cop
  @departments[cop.department] ||= []
  @departments[cop.department] << cop
end
length() click to toggle source
# File lib/rubocop/cop/registry.rb, line 112
def length
  @registry.size
end
names() click to toggle source
# File lib/rubocop/cop/registry.rb, line 122
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 = RuboCop::Cop::Cop.all
cops.
  qualified_cop_name('Layout/IndentArray') # => 'Layout/IndentArray'

@example fixes incorrect namespaces

cops = RuboCop::Cop::Cop.all
cops.qualified_cop_name('Lint/IndentArray') # => 'Layout/IndentArray'

@example namespaces bare cop identifiers

cops = RuboCop::Cop::Cop.all
cops.qualified_cop_name('IndentArray') # => 'Layout/IndentArray'

@example passes back unrecognized cop names

cops = RuboCop::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/rubocop/cop/registry.rb, line 91
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/rubocop/cop/registry.rb, line 136
def select(&block)
  cops.select(&block)
end
sort!() click to toggle source
# File lib/rubocop/cop/registry.rb, line 130
def sort!
  @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }]

  self
end
to_h() click to toggle source
# File lib/rubocop/cop/registry.rb, line 104
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/rubocop/cop/registry.rb, line 43
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/rubocop/cop/registry.rb, line 48
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/rubocop/cop/registry.rb, line 150
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/rubocop/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/rubocop/cop/registry.rb, line 156
def resolve_badge(given_badge, real_badge, source_path)
  unless given_badge.match?(real_badge)
    path = PathUtil.smart_path(source_path)
    warn "#{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/rubocop/cop/registry.rb, line 146
def with(cops)
  self.class.new(cops)
end