Module: RuboCop::Cop::LazyLoader

Included in:
Bundler, Gemspec, Layout, Lint, Metrics, Migration, Naming, Security, Style
Defined in:
lib/rubocop/cop/lazy_loader.rb

Overview

Extend a department module with this module and call register_cop to register the department's cops with the global registry without loading their files until a cop class itself is needed:

module RuboCop
module Cop
  # The Foo department.
  module Foo
    extend LazyLoader

    register_cop :BarBaz, "#{__dir__}/foo/bar_baz"
  end
end
end

Instance Method Summary collapse

Instance Method Details

#register_cop(constant_name, path) ⇒ Object

Registers a cop for lazy loading. The cop appears in the registry by name right away, but its file is loaded only when the cop class is needed, e.g. when the cop is enabled for an inspection run.

The path must be absolute so that loading does not depend on $LOAD_PATH and cannot resolve to another RuboCop installation.

Parameters:

  • constant_name (Symbol)

    name of the cop class, e.g. :BarBaz

  • path (String)

    absolute path to the file defining the cop class, with or without the .rb extension

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/lazy_loader.rb', line 30

def register_cop(constant_name, path)
  raise ArgumentError, "cop path must be absolute: #{path}" unless File.absolute_path?(path)

  autoload constant_name, path

  qualified_name = "#{name}::#{constant_name}"

  Registry.global.lazy_load(Badge.for(qualified_name), qualified_name)
end