class Path
Public Class Methods
[](p)
click to toggle source
# File lib/extensions.rb, line 46 def self.[](p) Path.new(p) end
new(*args)
click to toggle source
# File lib/extensions.rb, line 48 def initialize(*args) old_init(*args); @rc={}; @rc2={} end
Also aliased as: old_init
Public Instance Methods
**(p)
click to toggle source
# File lib/extensions.rb, line 51 def ** (p) self+p.to_p end
===(p)
click to toggle source
# File lib/extensions.rb, line 58 def ===(p) real == p.real end
[](p)
click to toggle source
# File lib/extensions.rb, line 65 def [](p) glob(p) end
_abs_i(p,wd)
click to toggle source
# File lib/extensions.rb, line 118 def _abs_i(p,wd) str = wd + '/' + p ; last = str[-1].chr combo = [] str.split('/').each do |part| case part when part.blank?, '.' then next when '..' then combo.pop else combo << part end end Path.new('/' + combo.join('/') + (last == '/' ? '/' : '')) end
abs(wd=nil)
click to toggle source
alias old_mm method_missing def method_missing(m,*a,&b) to_s.respond_to?(m) ? to_s.send(m,*a,&b) : old_mm(m,*a,&b) end
# File lib/extensions.rb, line 106 def abs(wd=nil) wd ||= ($pwd || Path.pwd); wd = wd.to_s s = self.to_s raise ArgumentError.new('Bad working directory- must be absolute') if wd[0].chr != '/' if s.blank? ; return nil elsif s[0].chr=='/' ; return s elsif s[0].chr=='~' && (s[1].nil?||s[1].chr=='/'); _abs_i(s[1..-1], ENV['HOME']) elsif s =~ /~([^\/]+)/; _abs_i($', Etc.getpwnam($1).dir) else _abs_i(s, wd) end end
as_other(new_dir, new_ext=nil)
click to toggle source
# File lib/extensions.rb, line 69 def as_other(new_dir, new_ext=nil) p = new_dir.nil? ? self : (new_dir.to_p + self.basename) p = Path[p.to_s.sub(/#{self.extname}$/,'.'+new_ext)] if new_ext return p end
different_contents?(str)
click to toggle source
# File lib/extensions.rb, line 81 def different_contents?(str) IO.read(self).strip != str.strip end
dir()
click to toggle source
# File lib/extensions.rb, line 62 def dir() (exp.dir? || to_s[-1].chr == '/') ? exp : exp.dirname end
dir!()
click to toggle source
# File lib/extensions.rb, line 63 def dir!() (exp.mkpath unless exp.dir? rescue return nil); self end
dir?()
click to toggle source
# File lib/extensions.rb, line 57 def dir?() directory? end
dist_from(p)
click to toggle source
# File lib/extensions.rb, line 98 def dist_from(p) return 0 if self === p travp = p.dir.rel(self.dir,false).to_s return 1 if travp =~ /^\/?\.\/?$/ return travp.split('/').size + 1 end
exp()
click to toggle source
# File lib/extensions.rb, line 60 def exp () return @exp ||= self.expand_path end
glob(g,&b)
click to toggle source
# File lib/extensions.rb, line 64 def glob(g,&b) Path.glob((dir + g.to_s).to_s, File::FNM_DOTMATCH, &b) end
missing?()
click to toggle source
# File lib/extensions.rb, line 68 def missing?() !self.exist? end
older_than?(p)
click to toggle source
def [](p) Path.glob
((dir + p.to_s).to_s, File::FNM_DOTMATCH) end
# File lib/extensions.rb, line 67 def older_than?(p) self.stat.mtime < p.stat.mtime end
perm?()
click to toggle source
# File lib/extensions.rb, line 59 def perm?() exp.dir? ? rwx? : rw? end
r?()
click to toggle source
# File lib/extensions.rb, line 52 def r? () readable_real? end
real()
click to toggle source
# File lib/extensions.rb, line 61 def real() begin exp.realpath rescue exp end end def dir() (exp.dir? || to_s[-1].chr == '/') ? exp : exp.dirname end def dir!() (exp.mkpath unless exp.dir? rescue return nil); self end def glob(g,&b) Path.glob((dir + g.to_s).to_s, File::FNM_DOTMATCH, &b) end def [](p) glob(p) end #def [](p) Path.glob((dir + p.to_s).to_s, File::FNM_DOTMATCH) end def older_than?(p) self.stat.mtime < p.stat.mtime end def missing?() !self.exist? end def as_other(new_dir, new_ext=nil) p = new_dir.nil? ? self : (new_dir.to_p + self.basename) p = Path[p.to_s.sub(/#{self.extname}$/,'.'+new_ext)] if new_ext return p end def rel(p=nil,home=true) p ||= ($pwd || Path.pwd) return @rc2[p] if @rc2[p] r = abs.rel_path_from(p.abs) r = r.sub(ENV['HOME'],'~') if home r end def different_contents?(str) IO.read(self).strip != str.strip end def short(p=nil,home=true) p ||= ($pwd || Path.pwd) return @rc2[p.to_s] if @rc2[p.to_s] sr = real; pr = p.real se = exp; pe = p.exp candidates = [sr.rel_path_from(pr), sr.rel_path_from(pe), se.rel_path_from(pr), se.rel_path_from(pe)] candidates += [sr.sub(ENV['HOME'],'~'), se.sub(ENV['HOME'],'~')] if home @rc2[p.to_s] = candidates.sort_by{|v|v.to_s.size}[0] end def rel_path_from(p) @rc ||= {}; @rc[p.to_s] ||= relative_path_from(p) end def relation_to(p) travp = p.rel(self,false).to_s if travp =~ /^(..\/)+..(\/|$)/ then :child else travp =~ /^..\// ? :stranger : :parent end end def dist_from(p) return 0 if self === p travp = p.dir.rel(self.dir,false).to_s return 1 if travp =~ /^\/?\.\/?$/ return travp.split('/').size + 1 end #alias old_mm method_missing #def method_missing(m,*a,&b) to_s.respond_to?(m) ? to_s.send(m,*a,&b) : old_mm(m,*a,&b) end def abs(wd=nil) wd ||= ($pwd || Path.pwd); wd = wd.to_s s = self.to_s raise ArgumentError.new('Bad working directory- must be absolute') if wd[0].chr != '/' if s.blank? ; return nil elsif s[0].chr=='/' ; return s elsif s[0].chr=='~' && (s[1].nil?||s[1].chr=='/'); _abs_i(s[1..-1], ENV['HOME']) elsif s =~ /~([^\/]+)/; _abs_i($', Etc.getpwnam($1).dir) else _abs_i(s, wd) end end private def _abs_i(p,wd) str = wd + '/' + p ; last = str[-1].chr combo = [] str.split('/').each do |part| case part when part.blank?, '.' then next when '..' then combo.pop else combo << part end end Path.new('/' + combo.join('/') + (last == '/' ? '/' : '')) end end class String def to_p() Path.new(self) end #alias old_mm method_missing #def method_missing(m,*a,&b) to_p.respond_to?(m) ? to_p.send(m,*a,&b) : old_mm(m,*a,&b) end #def respond_to_missing?(m,p=false) to_p.respond_to?(m,p) end def same_path(p) to_p === p end end end
rel(p=nil,home=true)
click to toggle source
# File lib/extensions.rb, line 74 def rel(p=nil,home=true) p ||= ($pwd || Path.pwd) return @rc2[p] if @rc2[p] r = abs.rel_path_from(p.abs) r = r.sub(ENV['HOME'],'~') if home r end
rel_path_from(p)
click to toggle source
# File lib/extensions.rb, line 92 def rel_path_from(p) @rc ||= {}; @rc[p.to_s] ||= relative_path_from(p) end
relation_to(p)
click to toggle source
# File lib/extensions.rb, line 93 def relation_to(p) travp = p.rel(self,false).to_s if travp =~ /^(..\/)+..(\/|$)/ then :child else travp =~ /^..\// ? :stranger : :parent end end
rw?()
click to toggle source
# File lib/extensions.rb, line 55 def rw? () r? && w? end
rwx?()
click to toggle source
# File lib/extensions.rb, line 56 def rwx?() r? && w? && x? end
short(p=nil,home=true)
click to toggle source
# File lib/extensions.rb, line 82 def short(p=nil,home=true) p ||= ($pwd || Path.pwd) return @rc2[p.to_s] if @rc2[p.to_s] sr = real; pr = p.real se = exp; pe = p.exp candidates = [sr.rel_path_from(pr), sr.rel_path_from(pe), se.rel_path_from(pr), se.rel_path_from(pe)] candidates += [sr.sub(ENV['HOME'],'~'), se.sub(ENV['HOME'],'~')] if home @rc2[p.to_s] = candidates.sort_by{|v|v.to_s.size}[0] end
to_p()
click to toggle source
# File lib/extensions.rb, line 50 def to_p() self end
w?()
click to toggle source
# File lib/extensions.rb, line 53 def w? () writable_real? end
x?()
click to toggle source
# File lib/extensions.rb, line 54 def x? () executable_real? end