Class: Sevgi::Function::Locate
- Inherits:
-
Object
- Object
- Sevgi::Function::Locate
- 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
-
#exclude ⇒ Object
readonly
Returns the value of attribute exclude.
-
#paths ⇒ Array<String>
readonly
Candidate paths.
-
#start ⇒ String
readonly
Start directory.
Class Method Summary collapse
-
.call(paths, start = Dir.pwd, exclude: nil) {|path| ... } ⇒ Sevgi::Function::Locate::Location?
Builds a locator and runs it.
Instance Method Summary collapse
-
#call {|path| ... } ⇒ Sevgi::Function::Locate::Location?
Runs the upward lookup.
-
#initialize(paths, start = ::Dir.pwd, exclude: nil) ⇒ void
constructor
Builds an upward file locator.
Constructor Details
#initialize(paths, start = ::Dir.pwd, exclude: nil) ⇒ void
Builds an upward file locator.
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.(it) } unless exclude.nil? end |
Instance Attribute Details
#exclude ⇒ Object (readonly)
Returns the value of attribute exclude.
28 |
# File 'lib/sevgi/function/locate.rb', line 28 attr_reader :paths, :start, :exclude |
#paths ⇒ Array<String> (readonly)
Returns candidate paths.
28 29 30 |
# File 'lib/sevgi/function/locate.rb', line 28 def paths @paths end |
#start ⇒ String (readonly)
Returns 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?
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.
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.(found, here), found, here] ensure ::Dir.chdir(origin) end |