Class: RuboCop::Cop::Generator::ConfigurationInjector Private
Relationships & Source Files | |
Inherits: | Object |
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
-
TEMPLATE =
# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 10<<~YAML %<badge>s: Description: 'TODO: Write a description of the cop.' Enabled: pending VersionAdded: '%<version_added>s' YAML
Class Method Summary
- .new(configuration_file_path:, badge:, version_added: '<<next>>') ⇒ ConfigurationInjector constructor Internal use only
Instance Attribute Summary
- #badge readonly private Internal use only
- #configuration_file_path readonly private Internal use only
- #output readonly private Internal use only
- #version_added readonly private Internal use only
Instance Method Summary
- #inject Internal use only
- #configuration_entries private Internal use only
- #cop_name_line?(yaml) ⇒ Boolean private Internal use only
- #find_target_line private Internal use only
- #new_configuration_entry private Internal use only
Constructor Details
.new(configuration_file_path:, badge:, version_added: '<<next>>') ⇒ ConfigurationInjector
# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 17
def initialize(configuration_file_path:, badge:, version_added: '<<next>>') @configuration_file_path = configuration_file_path @badge = badge @version_added = version_added @output = output end
Instance Attribute Details
#badge (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 39
attr_reader :configuration_file_path, :badge, :version_added, :output
#configuration_file_path (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 39
attr_reader :configuration_file_path, :badge, :version_added, :output
#output (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 39
attr_reader :configuration_file_path, :badge, :version_added, :output
#version_added (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 39
attr_reader :configuration_file_path, :badge, :version_added, :output
Instance Method Details
#configuration_entries (private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 41
def configuration_entries @configuration_entries ||= File.readlines(configuration_file_path) end
#cop_name_line?(yaml) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 59
def cop_name_line?(yaml) !/^[\s#]/.match?(yaml) end
#find_target_line (private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 49
def find_target_line configuration_entries.find.with_index do |line, index| next unless cop_name_line?(line) return index if badge.to_s < line end nil end
#inject
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 24
def inject target_line = find_target_line if target_line configuration_entries.insert(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
#new_configuration_entry (private)
[ GitHub ]# File 'lib/rubocop/cop/generator/configuration_injector.rb', line 45
def new_configuration_entry format(TEMPLATE, badge: badge, version_added: version_added) end