class RuboCop::Cop::Generator::RequireFileInjector
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
Constants
- REQUIRE_PATH
Attributes
Public Class Methods
Source
# 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
Public Instance Methods
Source
# 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
Private Instance Methods
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 64 def injectable_require_directive "require_relative '#{require_path}'\n" end
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 31 def require_exists? require_entries.any?(injectable_require_directive) end
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 68 def require_path path = source_path.relative_path_from(root_file_path.dirname) path.to_s.delete_suffix('.rb') end
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 58 def require_path_fragments(require_directive) path = require_directive.match(REQUIRE_PATH) path ? path.captures.first.split('/') : [] end
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 39 def target_line @target_line ||= begin in_the_same_department = false inject_parts = require_path_fragments(injectable_require_directive) require_entries.find.with_index do |entry, index| current_entry_parts = require_path_fragments(entry) if inject_parts[0..-2] == current_entry_parts[0..-2] in_the_same_department = true break index if inject_parts.last < current_entry_parts.last elsif in_the_same_department break index end end || require_entries.size end end
Source
# File lib/rubocop/cop/generator/require_file_injector.rb, line 35 def updated_directives require_entries.insert(target_line, injectable_require_directive).join end