ryz310/rubocop_challenger

View on GitHub
lib/rubocop_challenger/github/pr_template.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

module RubocopChallenger
  module Github
    # To generate Pull Request template as markdown
    class PrTemplate
      FOOTER_TEXT = 'Auto generated by [rubocop_challenger](https://github.com/ryz310/rubocop_challenger)'

      def initialize(rule, template_path = nil)
        template_path ||= default_template_path
        @template = File.read(template_path)
        @rule = rule
      end

      def generate
        <<~TEMPLATE
          #{ERB.new(template, trim_mode: '-').result(binding)}
          #{FOOTER_TEXT}
        TEMPLATE
      rescue StandardError => e
        error_template = File.read(error_template_path)
        <<~TEMPLATE
          #{ERB.new(error_template, trim_mode: '-').result(binding)}
          #{FOOTER_TEXT}
        TEMPLATE
      end

      private

      attr_reader :template, :rule

      def default_template_path
        File.expand_path('../../templates/default.md.erb', __dir__)
      end

      def error_template_path
        File.expand_path('../../templates/error.md.erb', __dir__)
      end

      def title
        rule.title
      end

      def rubydoc_url
        rule.rubydoc_url
      end

      def description
        yardoc.description
      end

      def examples
        yardoc.examples
      end

      def safe_autocorrect?
        yardoc.safe_autocorrect?
      end

      def yardoc
        @yardoc ||= Rubocop::Yardoc.new(title)
      end
    end
  end
end