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, *exts) ⇒ Object
Delete the tail.
-
#expand2(name, dir = ".") ⇒ String
Alias from File.expand_path.
-
#has_ext?(path, *exts) ⇒ Boolean
Return true if a path has the ext.
-
#parent2(path, n = 1) ⇒ String
get parent path.
-
#pwd2 ⇒ String
Return current work directory.
-
#real2(name, dir = ".") ⇒ String
Alias from Filel.realpath.
-
#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 ?
33 34 35 36 |
# File 'lib/pa/path.rb', line 33 def absolute?(path) p = get(path) File.absolute_path(p, ".") == p # rbx end |
#add_ext2(path, ext) ⇒ Object
Ensure the tail
153 154 155 156 157 158 159 160 161 |
# File 'lib/pa/path.rb', line 153 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.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pa/path.rb', line 44 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, *exts) ⇒ Object
Delete the tail.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pa/path.rb', line 133 def delete_ext2(path, *exts) p = get(path) exts.each {|ext| if has_ext?(p, ext) return p[0...p.rindex(ext)] end } p end |
#expand2(name, dir = ".") ⇒ String
Alias from File.expand_path
59 60 61 |
# File 'lib/pa/path.rb', line 59 def (name, dir=".") File.(get(name), dir) end |
#has_ext?(path, *exts) ⇒ Boolean
Return true if a path has the ext.
121 122 123 |
# File 'lib/pa/path.rb', line 121 def has_ext?(path, *exts) exts.include? Pa.ext2(get(path)) end |
#parent2(path, n = 1) ⇒ String
get parent path
189 190 191 192 193 194 195 |
# File 'lib/pa/path.rb', line 189 def parent2(path, n=1) path = get(path) n.times do path = File.dirname(path) end path end |
#pwd2 ⇒ String
Return current work directory
25 26 27 |
# File 'lib/pa/path.rb', line 25 def pwd2 Dir.getwd end |
#real2(name, dir = ".") ⇒ String
Alias from Filel.realpath
67 68 69 |
# File 'lib/pa/path.rb', line 67 def real2(name, dir=".") File.realpath(get(name), dir) end |
#relative_to2(path, dir) ⇒ String
Delete the head.
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pa/path.rb', line 101 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
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pa/path.rb', line 78 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/pa/path.rb', line 168 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 |