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(name, dir = ".") ⇒ 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(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 ?
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
152 153 154 155 156 157 158 159 160 |
# File 'lib/pa/path.rb', line 152 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.
134 135 136 137 138 139 140 141 142 |
# File 'lib/pa/path.rb', line 134 def delete_ext2(path, ext) p = get(path) if has_ext?(p, ext) p[0...p.rindex(ext)] else p end end |
#expand2(name, dir = ".") ⇒ String
Alias from File.expand_path
60 61 62 |
# File 'lib/pa/path.rb', line 60 def (name, dir=".") File.(get(name), dir) end |
#has_ext?(path, ext) ⇒ Boolean
Return true if a path has the ext.
122 123 124 |
# File 'lib/pa/path.rb', line 122 def has_ext?(path, ext) Pa.ext2(get(path)) == ext end |
#parent2(path, n = 1) ⇒ String
get parent path
188 189 190 191 192 193 194 |
# File 'lib/pa/path.rb', line 188 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(name, dir = ".") ⇒ String
Alias from Filel.realpath
68 69 70 |
# File 'lib/pa/path.rb', line 68 def real2(name, dir=".") File.realpath(get(name), dir) end |
#relative_to2(path, dir) ⇒ String
Delete the head.
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pa/path.rb', line 102 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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pa/path.rb', line 79 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/pa/path.rb', line 167 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 |