Class: Sevgi::Function::Locate

Inherits:
Object
  • Object
show all
Defined in:
lib/sevgi/function/locate.rb

Overview

Locates one of several candidate files by walking upward from a start directory.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, start = ::Dir.pwd, exclude: nil) ⇒ void

Builds an upward file locator.

Parameters:

  • paths (Array<String>, String)

    candidate file paths

  • start (String) (defaults to: ::Dir.pwd)

    directory where lookup starts

  • exclude (Array<String>, String, nil) (defaults to: nil)

    paths ignored during lookup



35
36
37
38
39
# File 'lib/sevgi/function/locate.rb', line 35

def initialize(paths, start = ::Dir.pwd, exclude: nil)
  @paths = Array(paths)
  @start = start
  @exclude = [*exclude].map { ::File.expand_path(it) } unless exclude.nil?
end

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



28
# File 'lib/sevgi/function/locate.rb', line 28

attr_reader :paths, :start, :exclude

#pathsArray<String> (readonly)

Returns candidate paths.

Returns:

  • (Array<String>)

    candidate paths



28
29
30
# File 'lib/sevgi/function/locate.rb', line 28

def paths
  @paths
end

#startString (readonly)

Returns start directory.

Returns:

  • (String)

    start directory



28
# File 'lib/sevgi/function/locate.rb', line 28

attr_reader :paths, :start, :exclude

Class Method Details

.call(paths, start = Dir.pwd, exclude: nil) {|path| ... } ⇒ Sevgi::Function::Locate::Location?

Builds a locator and runs it.

Parameters:

  • paths (Array<String>, String)

    candidate file paths

  • start (String) (defaults to: Dir.pwd)

    directory where lookup starts

  • exclude (Array<String>, String, nil) (defaults to: nil)

    paths ignored during lookup

Yields:

  • optional matcher used instead of file existence checks

Yield Parameters:

  • path (String)

    candidate path

Yield Returns:

  • (Boolean)

Returns:

  • (Sevgi::Function::Locate::Location, nil)

    found location, or nil



16
# File 'lib/sevgi/function/locate.rb', line 16

def self.call(*, **, &block) = new(*, **).call(&block)

Instance Method Details

#call {|path| ... } ⇒ Sevgi::Function::Locate::Location?

Runs the upward lookup.

Yields:

  • optional matcher used instead of file existence checks

Yield Parameters:

  • path (String)

    candidate path

Yield Returns:

  • (Boolean)

Returns:

  • (Sevgi::Function::Locate::Location, nil)

    found location, or nil

Raises:

  • (Errno::ENOENT)

    when the start directory cannot be entered



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sevgi/function/locate.rb', line 47

def call(&block)
  origin = ::Dir.pwd
  ::Dir.chdir(start)

  here = ::Dir.pwd
  until (found = match(&block))
    ::Dir.chdir("..")
    ::Dir.pwd == here ? return : here = ::Dir.pwd
  end

  Location[::File.expand_path(found, here), found, here]
ensure
  ::Dir.chdir(origin)
end