Class: MVCLI::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/mvcli/path.rb

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Path

Returns a new instance of Path.



5
6
7
# File 'lib/mvcli/path.rb', line 5

def initialize(base)
  @base = Pathname(base.to_s)
end

Instance Method Details

#ancestors(dir = @base) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/mvcli/path.rb', line 29

def ancestors(dir = @base)
  if dir == dir.parent
    []
  else
    [dir] + ancestors(dir.parent)
  end
end

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/mvcli/path.rb', line 9

def exists?(path)
  @base.join(path).exist?
end

#join(path) ⇒ Object



17
18
19
# File 'lib/mvcli/path.rb', line 17

def join(path)
  self.class.new @base.join path
end

#nearest(pattern) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/mvcli/path.rb', line 21

def nearest(pattern)
  ancestors.each do |dir|
    if entry = dir.entries.find { |e| e.to_s.match pattern }
      return dir.join entry
    end
  end
end

#read(path) ⇒ Object



13
14
15
# File 'lib/mvcli/path.rb', line 13

def read(path)
  @base.join(path).read
end

#to_s(path = nil) ⇒ Object



37
38
39
# File 'lib/mvcli/path.rb', line 37

def to_s(path = nil)
  path.nil? ? @base.to_s : @base.join(path).to_s
end