class RuboCop::Cop::Generator::RequireFileInjector
namespace and injects the provided one in alpha
It looks for other directives that require files in the same (cop)
A class that injects a require directive into the root RuboCop file.
def initialize(require_path)
def initialize(require_path) @require_path = require_path @require_entries = File.readlines(rubocop_root_file_path) end
def inject
def inject return if require_exists? || !target_line File.write(rubocop_root_file_path, updated_directives) end
def injectable_require_directive
def injectable_require_directive "require '#{require_path}'\n" end
def require_exists?
def require_exists? require_entries.any? do |entry| entry == injectable_require_directive end end
def require_path_fragments(require_directove)
def require_path_fragments(require_directove) path = require_directove.match(REQUIRE_PATH) path ? path.captures.first.split('/') : [] end
def rubocop_root_file_path
def rubocop_root_file_path File.join('lib', 'rubocop.rb') end
def target_line
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 end end
def updated_directives
def updated_directives require_entries.insert(target_line, injectable_require_directive).join end