Sha256: 08468b446e95f6739bafd6848e36865a2acf27c48539ab2267c2ca1f974ebc44

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module RuboCop
  # Handles caching of configurations and association of inspected
  # ruby files to configurations.
  class ConfigStore
    def initialize
      # @options_config stores a config that is specified in the command line.
      # This takes precedence over configs located in any directories
      @options_config = nil

      # @path_cache maps directories to configuration paths. We search
      # for .rubocop.yml only if we haven't already found it for the
      # given directory.
      @path_cache = {}

      # @object_cache maps configuration file paths to
      # configuration objects so we only need to load them once.
      @object_cache = {}
    end

    def options_config=(options_config)
      loaded_config = ConfigLoader.load_file(options_config)
      @options_config = ConfigLoader.merge_with_default(loaded_config,
                                                        options_config)
    end

    def force_default_config!
      @options_config = ConfigLoader.default_configuration
    end

    def for_file(file)
      for_dir(File.dirname(file))
    end

    # If type (file/dir) is known beforehand,
    # prefer using #for_file or #for_dir for improved performance
    def for(file_or_dir)
      dir = if File.directory?(file_or_dir)
              file_or_dir
            else
              File.dirname(file_or_dir)
            end
      for_dir(dir)
    end

    def for_dir(dir)
      return @options_config if @options_config

      @path_cache[dir] ||= ConfigLoader.configuration_file_for(dir)
      path = @path_cache[dir]
      @object_cache[path] ||= begin
                                print "For #{dir}: " if ConfigLoader.debug?
                                ConfigLoader.configuration_from_file(path)
                              end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
rbhint-0.87.1.rc1 lib/rubocop/config_store.rb
rubocop-0.87.1 lib/rubocop/config_store.rb
rubocop-0.87.0 lib/rubocop/config_store.rb
rubocop-0.86.0 lib/rubocop/config_store.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/config_store.rb
rbhint-0.85.1.rc2 lib/rubocop/config_store.rb
rbhint-0.85.1.rc1 lib/rubocop/config_store.rb
rubocop-0.85.1 lib/rubocop/config_store.rb
rbhint-0.8.5.rc1 lib/rubocop/config_store.rb
rubocop-0.85.0 lib/rubocop/config_store.rb