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

require_entries[R]
require_path[R]

Public Class Methods

new(require_path) click to toggle source
# File lib/rubocop/cop/generator.rb, line 179
def initialize(require_path)
  @require_path    = require_path
  @require_entries = File.readlines(rubocop_root_file_path)
end

Public Instance Methods

inject() click to toggle source
# File lib/rubocop/cop/generator.rb, line 184
def inject
  return if require_exists? || !target_line

  File.write(rubocop_root_file_path, updated_directives)
end

Private Instance Methods

injectable_require_directive() click to toggle source
# File lib/rubocop/cop/generator.rb, line 234
def injectable_require_directive
  "require_relative '#{require_path}'\n"
end
require_exists?() click to toggle source
# File lib/rubocop/cop/generator.rb, line 198
def require_exists?
  require_entries.any? do |entry|
    entry == injectable_require_directive
  end
end
require_path_fragments(require_directove) click to toggle source
# File lib/rubocop/cop/generator.rb, line 228
def require_path_fragments(require_directove)
  path = require_directove.match(REQUIRE_PATH)

  path ? path.captures.first.split('/') : []
end
rubocop_root_file_path() click to toggle source
# File lib/rubocop/cop/generator.rb, line 194
def rubocop_root_file_path
  File.join('lib', 'rubocop.rb')
end
target_line() click to toggle source
# File lib/rubocop/cop/generator.rb, line 209
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
updated_directives() click to toggle source
# File lib/rubocop/cop/generator.rb, line 204
def updated_directives
  require_entries.insert(target_line,
                         injectable_require_directive).join
end