Method: QB::Util.find_up

Defined in:
lib/qb/util.rb

.find_up(filename, from = Pathname.pwd, raise_on_not_found: true) ⇒ Pathname?

Find filename in from or closest parent directory.

Parameters:

  • filename (String)

    name of file to search for.

  • from (Pathname) (defaults to: Pathname.pwd)

    (Pathname.pwd) directory to start from.

  • raise_on_not_found: (Boolean) (defaults to: true)

    When true, a FSStateError will be raised if no file is found (default behavior).

    This is something of a legacy behavior - I think it would be better to have find_up return nil in that case and add a find_up! method that raises on not found. But I'm not going to do it right now.

Returns:

  • (Pathname)

    Pathname of found file.

  • (nil)

    If no file is found and the raise_on_not_found option is false.

Raises:

  • (QB::FSStateError)

    If file is not found in from or any of it's parent directories and the raise_on_not_found option is true (default behavior).



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/qb/util.rb', line 118

def self.find_up filename, from = Pathname.pwd, raise_on_not_found: true
  path = from + filename
  
  return from if path.exist?
  
  parent = from.parent
  
  if from == parent
    if raise_on_not_found
      raise "not found in current or any parent directories: #{ filename }"
    else
      return nil
    end
  end
  
  return find_up filename, parent, raise_on_not_found: raise_on_not_found
end