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

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

Overview

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 =
/require_relative ['"](.+)['"]/.freeze

Instance Method Summary collapse

Constructor Details

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

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



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