Class: RuboCop::Cop::Generator::RequireFileInjector Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/generator/require_file_injector.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class that injects a require directive into the root RuboCop file. It looks for other directives that require files in the same (cop) namespace and injects the provided one in alpha

Constant Summary collapse

REQUIRE_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/require_relative ['"](.+)['"]/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source_path:, root_file_path:, output: $stdout) ⇒ RequireFileInjector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RequireFileInjector.



12
13
14
15
16
17
# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 12

def initialize(source_path:, root_file_path:, output: $stdout)
  @source_path = Pathname(source_path)
  @root_file_path = Pathname(root_file_path)
  @require_entries = File.readlines(root_file_path)
  @output = output
end

Instance Method Details

#injectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
22
23
24
25
# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 19

def inject
  return if require_exists? || !target_line

  File.write(root_file_path, updated_directives)
  require = injectable_require_directive.chomp
  output.puts "[modify] #{root_file_path} - `#{require}` was injected."
end