Sha256: ae65dfa573af4c86871bf4ad5da917da08bcd6be4d56303affdf17af78a89a14

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

require 'yaml'

module RuboCop
  module RSpec
    # Builds a YAML config file from two config hashes
    class ConfigFormatter
      EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
      STYLE_GUIDE_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/'

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

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

      private

      def unified_config
        cops.each_with_object(config.dup) do |cop, unified|
          unified[cop] = config.fetch(cop)
            .merge(descriptions.fetch(cop))
            .merge('StyleGuide' => STYLE_GUIDE_BASE_URL + cop.sub('RSpec/', ''))
        end
      end

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

      attr_reader :config, :descriptions
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rspec-2.5.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.4.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.3.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.2.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.1.0 lib/rubocop/rspec/config_formatter.rb
rubocop-rspec-2.0.1 lib/rubocop/rspec/config_formatter.rb