123456789_123456789_123456789_123456789_123456789_

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(configuration_file_path:, badge:, version_added: '<<next>>') ⇒ ConfigurationInjector

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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