Sha256: e1a642b57b5d65e55bda241eff50a6fdac55f38538c41432d9d465e242684ec5

Contents?: true

Size: 1.34 KB

Versions: 15

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'yaml'

module RuboCop
  module FactoryBot
    # Builds a YAML config file from two config hashes
    class ConfigFormatter
      EXTENSION_ROOT_DEPARTMENT = %r{^(FactoryBot/)}.freeze
      SUBDEPARTMENTS = [].freeze
      AMENDMENTS = [].freeze
      COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-factory_bot/RuboCop/Cop/'

      def initialize(config, descriptions)
        @config       = config
        @descriptions = descriptions
      end

      def dump
        YAML.dump(unified_config)
          .gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
          .gsub(/^(\s+)- /, '\1  - ')
          .gsub('"~"', '~')
      end

      private

      def unified_config
        cops.each_with_object(config.dup) do |cop, unified|
          next if SUBDEPARTMENTS.include?(cop) || AMENDMENTS.include?(cop)

          replace_nil(unified[cop])
          unified[cop].merge!(descriptions.fetch(cop))
          unified[cop]['Reference'] = reference(cop)
        end
      end

      def cops
        (descriptions.keys | config.keys).grep(EXTENSION_ROOT_DEPARTMENT)
      end

      def replace_nil(config)
        config.each do |key, value|
          config[key] = '~' if value.nil?
        end
      end

      def reference(cop)
        COP_DOC_BASE_URL + cop
      end

      attr_reader :config, :descriptions
    end
  end
end

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-factory_bot-2.26.1/lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.26.1 lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.26.0 lib/rubocop/factory_bot/config_formatter.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.25.1/lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.25.1 lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.25.0 lib/rubocop/factory_bot/config_formatter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.24.0 lib/rubocop/factory_bot/config_formatter.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/factory_bot/config_formatter.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/factory_bot/config_formatter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/factory_bot/config_formatter.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-factory_bot-2.23.1/lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.23.1 lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.23.0 lib/rubocop/factory_bot/config_formatter.rb
rubocop-factory_bot-2.22.0 lib/rubocop/factory_bot/config_formatter.rb