lib/reek/configuration/configuration_file_finder.rb in reek-3.0.4 vs lib/reek/configuration/configuration_file_finder.rb in reek-3.1

- old
+ new

@@ -1,9 +1,10 @@ require 'pathname' module Reek module Configuration + 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 @@ -15,10 +16,14 @@ # configuration file is exactly like above. # @api private module ConfigurationFileFinder module_function + def find_and_load(params = {}) + load_from_file(find(params)) + end + # FIXME: switch to kwargs on upgrade to Ruby 2 and drop `params.fetch` calls: # def find(options: nil, current: Pathname.pwd, home: Pathname.new(Dir.home)) def find(params = {}) options = params.fetch(:options) { nil } current = params.fetch(:current) { Pathname.pwd } @@ -34,9 +39,23 @@ start.ascend do |dir| files = dir.children.select(&:file?).sort found = files.find { |file| file.to_s.end_with?('.reek') } return found if found end + end + + 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