Class: Sevgi::Function::Locate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Locate.



14
15
16
17
18
# File 'lib/sevgi/function/locate.rb', line 14

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.



12
13
14
# File 'lib/sevgi/function/locate.rb', line 12

def exclude
  @exclude
end

#pathsObject (readonly)

Returns the value of attribute paths.



12
13
14
# File 'lib/sevgi/function/locate.rb', line 12

def paths
  @paths
end

#startObject (readonly)

Returns the value of attribute start.



12
13
14
# File 'lib/sevgi/function/locate.rb', line 12

def start
  @start
end

Class Method Details

.call(&block) ⇒ Object



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

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

Instance Method Details

#call(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sevgi/function/locate.rb', line 20

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