class RuboCop::Cop::Generator
Source and spec generator for new cops
This generator will take a cop name and generate a source file and spec file when given a valid qualified cop name.
Constants
- SOURCE_TEMPLATE
- SPEC_TEMPLATE
Attributes
badge[R]
Public Class Methods
new(name)
click to toggle source
# File lib/rubocop/cop/generator.rb, line 79 def initialize(name) @badge = Badge.parse(name) return if badge.qualified? raise ArgumentError, 'Specify a cop name with Department/Name style' end
Public Instance Methods
inject_require()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 95 def inject_require RequireFileInjector.new(require_path).inject end
todo()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 99 def todo <<-TODO.strip_indent Files created: - #{source_path} - #{spec_path} File modified: - `require_relative '#{require_path}'` added into lib/rubocop.rb Do 3 steps: 1. Add an entry to the "New features" section in CHANGELOG.md, e.g. "Add new `#{badge}` cop. ([@your_id][])" 2. Add an entry into config/enabled.yml or config/disabled.yml 3. Implement your new cop in the generated file! TODO end
write_source()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 87 def write_source write_unless_file_exists(source_path, generated_source) end
write_spec()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 91 def write_spec write_unless_file_exists(spec_path, generated_spec) end
Private Instance Methods
generate(template)
click to toggle source
# File lib/rubocop/cop/generator.rb, line 139 def generate(template) format(template, department: badge.department, cop_name: badge.cop_name) end
generated_source()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 131 def generated_source generate(SOURCE_TEMPLATE) end
generated_spec()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 135 def generated_spec generate(SPEC_TEMPLATE) end
require_path()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 143 def require_path source_path.sub('lib/', '').sub('.rb', '') end
snake_case(camel_case_string)
click to toggle source
# File lib/rubocop/cop/generator.rb, line 167 def snake_case(camel_case_string) camel_case_string .gsub(/([^A-Z])([A-Z]+)/, '\1_\2') .gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2') .downcase end
source_path()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 157 def source_path File.join( 'lib', 'rubocop', 'cop', snake_case(badge.department.to_s), "#{snake_case(badge.cop_name.to_s)}.rb" ) end
spec_path()
click to toggle source
# File lib/rubocop/cop/generator.rb, line 147 def spec_path File.join( 'spec', 'rubocop', 'cop', snake_case(badge.department.to_s), "#{snake_case(badge.cop_name.to_s)}_spec.rb" ) end
write_unless_file_exists(path, contents)
click to toggle source
# File lib/rubocop/cop/generator.rb, line 119 def write_unless_file_exists(path, contents) if File.exist?(path) warn "rake new_cop: #{path} already exists!" exit! end dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.exist?(dir) File.write(path, contents) end