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, expanded without changing process cwd

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

    paths ignored during lookup after absolute expansion



41
42
43
44
45
# File 'lib/sevgi/function/locate.rb', line 41

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

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



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

attr_reader :paths, :start, :exclude

#pathsArray<String> (readonly)

Returns candidate paths.

Returns:

  • (Array<String>)

    candidate paths



34
35
36
# File 'lib/sevgi/function/locate.rb', line 34

def paths
  @paths
end

#startString (readonly)

Returns absolute start directory.

Returns:

  • (String)

    absolute start directory



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

attr_reader :paths, :start, :exclude

Class Method Details

.call(paths, start = Dir.pwd, exclude: nil) {|path| ... } ⇒ Sevgi::Function::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:



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

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

Instance Method Details

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

Runs the upward lookup.

Yields:

  • optional matcher used instead of file existence checks

Yield Parameters:

  • path (String)

    absolute candidate path

Yield Returns:

  • (Boolean)

Returns:

Raises:

  • (Errno::ENOENT)

    when the start directory does not exist

  • (Errno::ENOTDIR)

    when the start path is not a directory



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sevgi/function/locate.rb', line 54

def call(&block)
  validate_start!

  each_parent(start) do |here|
    next unless (found = match(here, &block))

    slug, file = found

    return Location[file, slug, here]
  end
end