Class: RuboCop::Cop::Generator::RequireFileInjector Private
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/cop/generator/require_file_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
-
REQUIRE_PATH =
# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 10/require_relative ['"](.+)['"]/.freeze
Class Method Summary
- .new(source_path:, root_file_path:, output: $stdout) ⇒ RequireFileInjector constructor Internal use only
Instance Attribute Summary
- #output readonly private Internal use only
- #require_entries readonly private Internal use only
- #require_exists? ⇒ Boolean readonly private Internal use only
- #root_file_path readonly private Internal use only
- #source_path readonly private Internal use only
Instance Method Summary
- #inject Internal use only
- #injectable_require_directive private Internal use only
- #require_path private Internal use only
- #require_path_fragments(require_directive) private Internal use only
- #target_line private Internal use only
- #updated_directives private Internal use only
Constructor Details
.new(source_path:, root_file_path:, output: $stdout) ⇒ RequireFileInjector
# 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
Instance Attribute Details
#output (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 29
attr_reader :require_entries, :root_file_path, :source_path, :output
#require_entries (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 29
attr_reader :require_entries, :root_file_path, :source_path, :output
#require_exists? ⇒ Boolean
(readonly, private)
# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 31
def require_exists? require_entries.any?(injectable_require_directive) end
#root_file_path (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 29
attr_reader :require_entries, :root_file_path, :source_path, :output
#source_path (readonly, private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 29
attr_reader :require_entries, :root_file_path, :source_path, :output
Instance Method Details
#inject
[ GitHub ]# 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
#injectable_require_directive (private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 64
def injectable_require_directive "require_relative '#{require_path}'\n" end
#require_path (private)
[ GitHub ]# 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
#require_path_fragments(require_directive) (private)
[ GitHub ]# 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
#target_line (private)
[ GitHub ]# 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
#updated_directives (private)
[ GitHub ]# File 'lib/rubocop/cop/generator/require_file_injector.rb', line 35
def updated_directives require_entries.insert(target_line, injectable_require_directive).join end