Sha256: dfced7ba7c5c5bfa717a0b6cbf06e7d13530d5655272f41a027843c0b1e3497a

Contents?: true

Size: 1.77 KB

Versions: 21

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true
require 'pathname'

module Reek
  module Configuration
    # Raised when config file is not properly readable.
    class ConfigFileException < StandardError; end
    #
    # ConfigurationFileFinder is responsible for finding Reek's configuration.
    #
    # There are 3 ways of passing `reek` a configuration file:
    # 1. Using the cli "-c" switch
    # 2. Having a file ending with .reek either in your current working
    #    directory or in a parent directory
    # 3. Having a file ending with .reek in your HOME directory
    #
    # The order in which ConfigurationFileFinder tries to find such a
    # configuration file is exactly like above.
    module ConfigurationFileFinder
      module_function

      def find_and_load(path: nil)
        load_from_file(find(path: path))
      end

      # :reek:ControlParameter
      def find(path: nil, current: Pathname.pwd, home: Pathname.new(Dir.home))
        path || find_by_dir(current) || find_in_dir(home)
      end

      def find_by_dir(start)
        start.ascend do |dir|
          found = find_in_dir(dir)
          return found if found
        end
      end

      def find_in_dir(dir)
        files = dir.children.select(&:file?).sort
        files.find { |file| file.to_s.end_with?('.reek') }
      end

      # :reek:TooManyStatements: { max_statements: 6 }
      def load_from_file(path)
        return {} unless path
        begin
          configuration = YAML.load_file(path) || {}
        rescue => error
          raise ConfigFileException, "Invalid configuration file #{path}, error is #{error}"
        end

        unless configuration.is_a? Hash
          raise ConfigFileException, "Invalid configuration file \"#{path}\" -- Not a hash"
        end
        configuration
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
reek-4.5.1 lib/reek/configuration/configuration_file_finder.rb
reek-4.5.0 lib/reek/configuration/configuration_file_finder.rb
reek-4.4.2 lib/reek/configuration/configuration_file_finder.rb
reek-4.4.1 lib/reek/configuration/configuration_file_finder.rb
reek-4.4.0 lib/reek/configuration/configuration_file_finder.rb
reek-4.3.0 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.5 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.4 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.3 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.2 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.1 lib/reek/configuration/configuration_file_finder.rb
reek-4.2.0 lib/reek/configuration/configuration_file_finder.rb
reek-4.1.1 lib/reek/configuration/configuration_file_finder.rb
reek-4.1.0 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.5 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.4 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.3 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.2 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.1 lib/reek/configuration/configuration_file_finder.rb
reek-4.0.0 lib/reek/configuration/configuration_file_finder.rb