Sha256: 1e3472f48cb11546d5fac4090337ebb682062fabd40d9362eabcc7ff2b6076be

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'pathname'

module Reek
  module Configuration
    #
    # 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
      class << self
        def find(options)
          configuration_by_cli(options) ||
            configuration_in_file_system    ||
            configuration_in_home_directory
        end

        private

        def configuration_by_cli(options)
          return unless options # Return gracefully allowing calls without app context
          config_file_option = options.config_file
          return unless config_file_option
          path_name = Pathname.new config_file_option
          raise ArgumentError, "Config file #{path_name} doesn't exist" unless path_name.exist?
          path_name
        end

        def configuration_in_file_system
          detect_or_traverse_up Pathname.pwd
        end

        def configuration_in_home_directory
          detect_configuration_in_directory Pathname.new(Dir.home)
        end

        def detect_or_traverse_up(directory)
          file = detect_configuration_in_directory(directory)
          return file unless file.nil?
          return if directory.root?
          detect_or_traverse_up directory.parent
        end

        def detect_configuration_in_directory(directory)
          directory.children.select(&:file?).find { |path| path.to_s.end_with?('.reek') }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reek-2.0.4 lib/reek/configuration/configuration_file_finder.rb