Class: RubomaticHtml::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rubomatic-html/generator.rb,
lib/rubomatic-html/generator/cop_readme_injector.rb,
lib/rubomatic-html/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 RubomaticHtml\n  module Cop\n    module %{department}\n      class %{cop_name} < RubomaticHtml::Cop::%{department}::Base\n        class << self\n          # @see super\n          def abstract_cop?\n            false\n          end\n\n          # @see super\n          def name\n            [department, '%{cop_name}'].join('/')\n          end\n        end\n\n        # @see super\n        def run_for_line(line, index)\n          # TODO: Implement the cop in here.\n        end\n      end\n    end\n  end\nend\n"
SPEC_TEMPLATE =
"# frozen_string_literal: true\n\nRSpec.describe RubomaticHtml::Cop::%{department}::%{cop_name}, :config do\n  let(:config) { RubomaticHtml::Config.new }\n\n  # TODO: Write test code\n  #\n  # For example\n  it 'registers an offense when using `#bad_method`' do\n    expect_offense(<<~RHTML)\n      bad_method\n      ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`.\n    RHTML\n  end\n\n  it 'does not register an offense when using `#good_method`' do\n    expect_no_offenses(<<~RHTML)\n      good_method\n    RHTML\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,rhtml]\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:



147
148
149
# File 'lib/rubomatic-html/generator.rb', line 147

def initialize(name, output: $stdout)
  @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:

  • (*)


202
203
204
# File 'lib/rubomatic-html/generator.rb', line 202

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

Instance Method Details

#generated_sourceObject

See Also:

  • method


162
163
164
# File 'lib/rubomatic-html/generator.rb', line 162

def generated_source
  generate(SOURCE_TEMPLATE)
end

#generated_specObject

See Also:

  • method


167
168
169
# File 'lib/rubomatic-html/generator.rb', line 167

def generated_spec
  generate(SPEC_TEMPLATE)
end

#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



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rubomatic-html/generator.rb', line 255

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



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rubomatic-html/generator.rb', line 237

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

#inject_require(root_file_path:) ⇒ Object

See Also:

  • method


194
195
196
# File 'lib/rubomatic-html/generator.rb', line 194

def inject_require(root_file_path:)
  RuboCop::Cop::Generator::RequireFileInjector.new(source_path: source_path, root_file_path: root_file_path).inject
end

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

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

Returns:

  • (Boolean)


208
209
210
# File 'lib/rubomatic-html/generator.rb', line 208

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

#source_pathObject

See Also:

  • method


172
173
174
175
176
177
178
179
180
# File 'lib/rubomatic-html/generator.rb', line 172

def source_path
  File.join(
    'lib',
    'rubomatic-html',
    'cop',
    snake_case(badge.department.to_s),
    "#{snake_case(badge.cop_name.to_s)}.rb"
  )
end

#spec_pathObject

See Also:

  • method


183
184
185
186
187
188
189
190
191
# File 'lib/rubomatic-html/generator.rb', line 183

def spec_path
  File.join(
    'spec',
    'rubomatic-html',
    'cop',
    snake_case(badge.department.to_s),
    "#{snake_case(badge.cop_name.to_s)}_spec.rb"
  )
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



228
229
230
# File 'lib/rubomatic-html/generator.rb', line 228

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



217
218
219
220
221
# File 'lib/rubomatic-html/generator.rb', line 217

def write_dept_readme
  return if File.exist?(dept_docs_path)

  write_unless_file_exists(dept_docs_path, generated_dept_docs)
end

#write_sourceObject

See Also:

  • method


152
153
154
# File 'lib/rubomatic-html/generator.rb', line 152

def write_source
  write_unless_file_exists(source_path, generated_source)
end

#write_specObject

See Also:

  • method


157
158
159
# File 'lib/rubomatic-html/generator.rb', line 157

def write_spec
  write_unless_file_exists(spec_path, generated_spec)
end