# encoding: utf-8 require 'fedux_org_stdlib/require_files' require 'fedux_org_stdlib/logging/logger' require_library %w(pathname) module FeduxOrgStdlib class RecursiveFileFinder # Create a new instance of finder # # It tries to find a file. # # @param [String] file_name # The name of the file to be looked for # # @param [String] working_directory # The directory where to start search # # @return [RecursiveFileFinder] # The config instance. If the resulting data structure created by the # config_engine does not respond to `:[]` an empty config object will be # created. attr_reader :working_directory, :logger, :file_name, :raise_error def initialize( file_name:, working_directory: Dir.getwd, logger: FeduxOrgStdlib::Logging::Logger.new, raise_error: true ) @logger = logger @working_directory = Pathname.new(working_directory).expand_path @file_name = Pathname.new(file_name) @raise_error = raise_error end # Return path to file # # @raise [Errno::ENOENT] # If file cannot be found # # @return [Pathname] # The path def file return @found_path if @found_path @found_path = nil working_directory.ascend do |p| path = p + file_name @found_path = path if path.exist? end return @found_path if @found_path fail Errno::ENOENT, file_name.to_s if raise_error nil end # The directory where file was found # # @raise [Errno::ENOENT] # If file cannot be found # # @return [Pathname] # The path to directory def directory return file.dirname if file fail Errno::ENOENT, file_name.to_s if raise_error nil end end end