Sha256: 93da08e68fdc1532416086302ae764aaea14e0b92515c7358ae9c68f76266542

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'rubocop'

module Slimembedcop
  # Generates a configuration for RuboCop.
  class ConfigGenerator
    def initialize(option)
      @default_config_path = option.default_config_path
      @forced_config_path = option.forced_config_path
    end

    def run
      ::RuboCop::ConfigLoader.merge_with_default(merged_config, loaded_path)
    end

    private

    def loaded_path
      @forced_config_path || implicit_config_path || @default_config_path
    end

    def merged_config
      ::RuboCop::Config.create(merged_config_hash, loaded_path)
    end

    def merged_config_hash
      result = default_config
      result = ::RuboCop::ConfigLoader.merge(result, user_config) if user_config
      result
    end

    def user_config
      if instance_variable_defined?(:@user_config)
        @user_config
      else
        @user_config =
          if @forced_config_path
            ::RuboCop::ConfigLoader.load_file(@forced_config_path)
          elsif (path = implicit_config_path)
            ::RuboCop::ConfigLoader.load_file(path)
          end
      end
    end

    def default_config
      ::RuboCop::ConfigLoader.load_file(@default_config_path)
    end

    def implicit_config_path
      if instance_variable_defined?(:@implicit_config_path)
        @implicit_config_path
      else
        @implicit_config_path = %w[.slimembedcop.yml].find do |path|
          ::File.exist?(path)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slimembedcop-1.0.0 lib/slimembedcop/config_generator.rb
slimembedcop-0.2.0 lib/slimembedcop/config_generator.rb