Module: Pa::Path::ClassMethods
- Defined in:
- lib/pa/path.rb
Instance Method Summary collapse
-
#absolute?(path) ⇒ Boolean
Is path an absolute path ?.
-
#add_ext2(path, ext) ⇒ Object
Ensure the tail.
-
#dangling?(path) ⇒ Boolean
Is path a dangling symlink?.
-
#delete_ext2(path, ext) ⇒ Object
Delete the tail.
-
#expand2(path) ⇒ String
Alias from File.expand_path.
-
#has_ext?(path, ext) ⇒ Boolean
Return true if a path has the ext.
-
#parent2(path, n = 1) ⇒ String
get parent path.
-
#pwd2 ⇒ String
Return current work directory.
-
#real2(path) ⇒ Object
real path.
-
#relative_to2(path, dir) ⇒ String
Delete the head.
-
#relative_to?(path, dir) ⇒ Boolean
Path relative_to? dir.
-
#shorten2(path) ⇒ String
shorten2 a path, convert /home/user/file to ~/file.
Instance Method Details
#absolute?(path) ⇒ Boolean
Is path an absolute path ?
34 35 36 37 |
# File 'lib/pa/path.rb', line 34 def absolute?(path) p = get(path) File.absolute_path(p, ".") == p # rbx end |
#add_ext2(path, ext) ⇒ Object
Ensure the tail
144 145 146 147 148 149 150 151 152 |
# File 'lib/pa/path.rb', line 144 def add_ext2(path, ext) p = get(path) if Pa.ext2(p) == ext p else "#{p}#{ext}" end end |
#dangling?(path) ⇒ Boolean
Is path a dangling symlink?
a dangling symlink is a dead symlink.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pa/path.rb', line 45 def dangling?(path) p = get(path) if File.symlink?(p) src = File.readlink(p) not File.exists?(src) else nil end end |
#delete_ext2(path, ext) ⇒ Object
Delete the tail.
126 127 128 129 130 131 132 133 134 |
# File 'lib/pa/path.rb', line 126 def delete_ext2(path, ext) p = get(path) if has_ext?(p, ext) p[0...p.rindex(ext)] else p end end |
#expand2(path) ⇒ String
Alias from File.expand_path
60 61 62 |
# File 'lib/pa/path.rb', line 60 def (path) File. get(path) end |
#has_ext?(path, ext) ⇒ Boolean
Return true if a path has the ext.
114 115 116 |
# File 'lib/pa/path.rb', line 114 def has_ext?(path, ext) Pa.ext2(get(path)) == ext end |
#parent2(path, n = 1) ⇒ String
get parent path
185 186 187 188 189 190 191 |
# File 'lib/pa/path.rb', line 185 def parent2(path, n=1) path = get(path) n.times do path = File.dirname(path) end path end |
#pwd2 ⇒ String
Return current work directory
26 27 28 |
# File 'lib/pa/path.rb', line 26 def pwd2 Dir.getwd end |
#real2(path) ⇒ Object
real path
176 177 178 |
# File 'lib/pa/path.rb', line 176 def real2(path) File.realpath get(path) end |
#relative_to2(path, dir) ⇒ String
Delete the head.
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pa/path.rb', line 94 def relative_to2(path, dir) p = get(path) if relative_to?(p, dir) path_parts = Pa.split(p, all: true) dir_parts = Pa.split(dir, all: true) ret = File.join(*path_parts[dir_parts.length..-1]) ret == "" ? "." : ret else p end end |
#relative_to?(path, dir) ⇒ Boolean
Path relative_to? dir
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/pa/path.rb', line 71 def relative_to?(path, dir) path_parts = Pa.split2(get(path), all: true) dir_parts = Pa.split2(get(dir), all: true) index = -1 dir_parts.all? {|part| index += 1 path_parts[index] == part } end |
#shorten2(path) ⇒ String
shorten2 a path, convert /home/user/file to ~/file
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pa/path.rb', line 159 def shorten2(path) p = get(path) home = Pa.home2 return p if home.empty? ret = relative_to2(p, home) if ret == p p else ret == "." ? "" : ret File.join("~", ret) end end |