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 =
<<~RUBY
  # TODO: Write cop description and example of bad / good code. For every
  # `SupportedStyle` and unique configuration, there needs to be examples.
  # Examples must have valid Ruby syntax. Do not use upticks.
  #
  # @safety
  #   Delete this section if the cop is not unsafe (`Safe: false` or
  #   `SafeAutoCorrect: false`), or use it to explain how the cop is
  #   unsafe.
  #
  # @example EnforcedStyle: bar (default)
  #   # Description of the `bar` style.
  #
  #   # bad
  #   bad_bar_method
  #
  #   # bad
  #   bad_bar_method(args)
  #
  #   # good
  #   good_bar_method
  #
  #   # good
  #   good_bar_method(args)
  #
  # @example EnforcedStyle: foo
  #   # Description of the `foo` style.
  #
  #   # bad
  #   bad_foo_method
  #
  #   # bad
  #   bad_foo_method(args)
  #
  #   # good
  #   good_foo_method
  #
  #   # good
  #   good_foo_method(args)
  #
RUBY
SOURCE_TEMPLATE =
<<~RUBY
  # frozen_string_literal: true

  module RubomaticHtml
    module Cop
      module %{department}
        class %{cop_name} < RubomaticHtml::Cop::%{department}::Base
          class << self
            # @see super
            def abstract_cop?
              false
            end

            # @see super
            def name
              [department, '%{cop_name}'].join('/')
            end
          end

          # @see super
          def run_for_line(line, index)
            # TODO: Implement the cop in here.
          end
        end
      end
    end
  end
RUBY
SPEC_TEMPLATE =
<<~SPEC
  # frozen_string_literal: true

  RSpec.describe RubomaticHtml::Cop::%{department}::%{cop_name}, :config do
    let(:config) { RubomaticHtml::Config.new }

    # TODO: Write test code
    #
    # For example
    it 'registers an offense when using `#bad_method`' do
      expect_offense(<<~RHTML)
        bad_method
        ^^^^^^^^^^ Use `#good_method` instead of `#bad_method`.
      RHTML
    end

    it 'does not register an offense when using `#good_method`' do
      expect_no_offenses(<<~RHTML)
        good_method
      RHTML
    end
  end
SPEC
README_ADDED_MESSAGE =
'[modify] A link for the %{dept_vs_cop} has been added into %{readme_file_path}.'
DEPT_README_TEMPLATE =
<<~ADOC
  = %{department}

  Describe the department here

  == Cops

ADOC
COP_README_TEMPLATE =
<<~ADOC
  = ``%{department}/%{cop_name}``

  == Description

  Add a description here

  == Examples

  [source,rhtml]
  ----
  <!-- Bad -->
  <!-- Add a bad example here -->

  <!-- Good -->
  <!-- Add a good example here -->
  ----

  == Configurable Attributes

  |===
  |Name |Default value |Configurable values

  |Max
  |120
  |Integer

  |===

  == References

  https://github.com/BrandsInsurance/expert-chainsaw/issues
ADOC

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