Class: RuboCop::Cop::Generator::ConfigurationInjector

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/generator/configuration_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

TEMPLATE =
<<~YAML
  %<badge>s:
    Description: 'TODO: Write a description of the cop.'
    Enabled: true
    VersionAdded: '%<version_added>s'
YAML

Instance Method Summary collapse

Constructor Details

#initialize(configuration_file_path:, badge:, version_added:) ⇒ ConfigurationInjector

Returns a new instance of ConfigurationInjector.



17
18
19
20
21
22
# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 17

def initialize(configuration_file_path:, badge:, version_added:)
  @configuration_file_path = configuration_file_path
  @badge = badge
  @version_added = version_added
  @output = output
end

Instance Method Details

#injectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 24

def inject
  target_line = find_target_line
  if target_line
    configuration_entries.insert(find_target_line,
                                 new_configuration_entry + "\n")
  else
    configuration_entries.push("\n" + new_configuration_entry)
  end

  File.write(configuration_file_path, configuration_entries.join)

  yield if block_given?
end