Module: FlossFunding::FileFinder Private

Included in:
ConfigFinder, ConfigLoader, Inclusion, Library
Defined in:
lib/floss_funding/file_finder.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Common methods for finding files.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.root_level=(level) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Set the artificial root level used to stop upward traversal when searching.

Parameters:

  • level (String, nil) —

    Absolute directory path or nil to clear



18
19
20
# File 'lib/floss_funding/file_finder.rb', line 18

def self.root_level=(level)
  @root_level = level
end

.root_level?(path, stop_dir) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check whether the given path has reached the configured root level or stop_dir.

Parameters:

  • path (String, Pathname) —

    The directory being checked during ascent

  • stop_dir (String, nil) —

    Optional hard stop directory when no root_level is set

Returns:

  • (Boolean) —

    true if traversal should stop at this path



27
28
29
# File 'lib/floss_funding/file_finder.rb', line 27

def self.root_level?(path, stop_dir)
  (@root_level || stop_dir) == path.to_s
end

Instance Method Details

#find_file_upwards(filename, start_dir, stop_dir = nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find the first occurrence of a file walking upward from a starting directory.

Parameters:

  • filename (String) —

    The basename or glob pattern to match (e.g., “Gemfile”, “*.gemspec”)

  • start_dir (String) —

    Absolute or relative directory to start from

  • stop_dir (String, nil) (defaults to: nil) —

    Optional directory at which to stop searching

Returns:

  • (String, nil) —

    Absolute path to the first matching file, or nil if none found



37
38
39
40
41
42
# File 'lib/floss_funding/file_finder.rb', line 37

def find_file_upwards(filename, start_dir, stop_dir = nil)
  traverse_files_upwards(filename, start_dir, stop_dir) do |file|
    # minimize iteration for performance
    return file if file
  end
end

#find_last_file_upwards(filename, start_dir, stop_dir = nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find the last occurrence of a file while walking upward from a starting directory.
Useful for selecting the nearest ancestor config file when multiple are present.

Parameters:

  • filename (String) —

    The basename or glob pattern to match (e.g., “Gemfile”, “*.gemspec”)

  • start_dir (String) —

    Absolute or relative directory to start from

  • stop_dir (String, nil) (defaults to: nil) —

    Optional directory at which to stop searching

Returns:

  • (String, nil) —

    Absolute path to the last matching file, or nil if none found



51
52
53
54
55
# File 'lib/floss_funding/file_finder.rb', line 51

def find_last_file_upwards(filename, start_dir, stop_dir = nil)
  last_file = nil
  traverse_files_upwards(filename, start_dir, stop_dir) { |file| last_file = file }
  last_file
end