Class: RuboCop::Cop::RubomaticRails::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rubomatic-rails/generator.rb,
lib/rubocop/cop/rubomatic-rails/generator/cop_readme_injector.rb,
lib/rubocop/cop/rubomatic-rails/generator/dept_readme_injector.rb

Defined Under Namespace

Classes: CopReadmeInjector, DeptReadmeInjector

Constant Summary collapse

COP_DOC =
"# TODO: Write cop description and example of bad / good code. For every\n# `SupportedStyle` and unique configuration, there needs to be examples.\n# Examples must have valid Ruby syntax. Do not use upticks.\n#\n# @safety\n#   Delete this section if the cop is not unsafe (`Safe: false` or\n#   `SafeAutoCorrect: false`), or use it to explain how the cop is\n#   unsafe.\n#\n# @example EnforcedStyle: bar (default)\n#   # Description of the `bar` style.\n#\n#   # bad\n#   bad_bar_method\n#\n#   # bad\n#   bad_bar_method(args)\n#\n#   # good\n#   good_bar_method\n#\n#   # good\n#   good_bar_method(args)\n#\n# @example EnforcedStyle: foo\n#   # Description of the `foo` style.\n#\n#   # bad\n#   bad_foo_method\n#\n#   # bad\n#   bad_foo_method(args)\n#\n#   # good\n#   good_foo_method\n#\n#   # good\n#   good_foo_method(args)\n#\n"
SOURCE_TEMPLATE =
"# frozen_string_literal: true\n\nmodule RuboCop\n  module Cop\n    module %{department}\n      class %{cop_name} < Base\n        # TODO: Implement the cop in here.\n        #\n        # In many cases, you can use a node matcher for matching node pattern.\n        # See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb\n        #\n        # For example\n        MSG = 'Use `#good_method` instead of `#bad_method`.'\n\n        # TODO: Don't call `on_send` unless the method name is in this list\n        # If you don't need `on_send` in the cop you created, remove it.\n        RESTRICT_ON_SEND = %%i[bad_method].freeze\n\n        # @!method bad_method?(node)\n        def_node_matcher :bad_method?, <<~PATTERN\n          (send nil? :bad_method ...)\n        PATTERN\n\n        def on_send(node)\n          return unless bad_method?(node)\n\n          add_offense(node)\n        end\n      end\n    end\n  end\nend\n"
README_ADDED_MESSAGE =
'[modify] A link for the %{dept_vs_cop} has been added into %{readme_file_path}.'
DEPT_README_TEMPLATE =
"= %{department}\n\nDescribe the department here\n\n== Cops\n\n"
COP_README_TEMPLATE =
"= ``%{department}/%{cop_name}``\n\n== Description\n\nAdd a description here\n\n== Examples\n\n[source,ruby]\n----\n# Bad\n# Add a bad example here\n\n# Good\n# Add a good example here\n----\n\n== Configurable Attributes\n\n|===\n|Name |Default value |Configurable values\n\n|Max\n|120\n|Integer\n\n|===\n\n== References\n\nhttps://github.com/BrandsInsurance/expert-chainsaw/issues\n"

Instance Method Summary collapse

Constructor Details

#initialize(name, output: $stdout) ⇒ Generator

:nodoc:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 131

def initialize(name, output: $stdout)
  name = ['RubomaticRails', name].join('/') unless name.start_with?('RubomaticRails/')

  unless name.count('/') == 2
    raise(
      [
        'You must provide a single department under RubomaticRails i.e. RubomaticRails/Department/CopName',
        'or Department/CopName'
      ].join(' ')
    )
  end

  @base_gen = RuboCop::Cop::Generator.new(name, output: output)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing*

Calls methods in the base class

Returns:

  • (*)


150
151
152
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 150

def method_missing(...)
  @base_gen.__send__(...)
end

Instance Method Details

#inject_cop_readme(readme_file_path: dept_docs_path) ⇒ void

This method returns an undefined value.

Injects the new cop readme link into the department readme Modified version of ‘inject_config` from RuboCop::Cop::Generator



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 203

def inject_cop_readme(readme_file_path: dept_docs_path)
  # Add this cop to the dept readme
  injector = CopReadmeInjector.new(
    readme_file_path: readme_file_path,
    badge: badge,
    department: department
  )

  injector.inject_string do
    output.puts(format(README_ADDED_MESSAGE, readme_file_path: readme_file_path, dept_vs_cop: 'cop'))
  end
end

#inject_dept_readme(readme_file_path: 'README.adoc') ⇒ void

This method returns an undefined value.

Injects the, possibly new, department readme link into the base readme Modified version of ‘inject_config` from RuboCop::Cop::Generator



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 185

def inject_dept_readme(readme_file_path: 'README.adoc')
  # Add this dept to base readme if not already there
  injector = DeptReadmeInjector.new(
    readme_file_path: readme_file_path,
    badge: badge,
    department: department
  )

  injector.inject_string do
    output.puts(format(README_ADDED_MESSAGE, readme_file_path: readme_file_path, dept_vs_cop: 'department'))
  end
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

‘self` responds to `method_name` if `@base_gen` does

Returns:

  • (Boolean)


156
157
158
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 156

def respond_to_missing?(method_name, include_private = false)
  @base_gen.respond_to?(method_name, include_private)
end

#write_cop_readmevoid

This method returns an undefined value.

Creates the cop readme if it doesn’t exist Modified version of ‘wirte_source` from RuboCop::Cop::Generator



176
177
178
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 176

def write_cop_readme
  write_unless_file_exists(docs_path, generated_cop_docs)
end

#write_dept_readmevoid

This method returns an undefined value.

Creates the department readme if it doesn’t exist Modified version of ‘wirte_source` from RuboCop::Cop::Generator



165
166
167
168
169
# File 'lib/rubocop/cop/rubomatic-rails/generator.rb', line 165

def write_dept_readme
  return if File.exist?(dept_docs_path)

  write_unless_file_exists(dept_docs_path, generated_dept_docs)
end