Sha256: a224a38dba2ae34b1b5ed478242fc7cd902763e35ff4ef6d4f72f2709486c810

Contents?: true

Size: 1.83 KB

Versions: 27

Compression:

Stored size: 1.83 KB

Contents

# encoding: utf-8
require 'fedux_org_stdlib/require_files'
require 'fedux_org_stdlib/logging/logger'

require_library %w(pathname)

module FeduxOrgStdlib
  # Look for file recursively
  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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.11.18 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.17 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.16 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.15 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.14 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.12 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.11 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.9 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.8 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.7 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.6 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.5 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.4 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.3 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.2 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.1 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.11.0 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.10.9 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.10.8 lib/fedux_org_stdlib/recursive_file_finder.rb
fedux_org-stdlib-0.10.7 lib/fedux_org_stdlib/recursive_file_finder.rb