Module: Pa::ClassMethods::Path
- Included in:
- Pa
- Defined in:
- lib/tagen/core/pa/path.rb
Instance Method Summary collapse
-
#absolute(path) ⇒ String
alias from File.absolute_path.
-
#absolute?(path) ⇒ Boolean
is path an absolute path ?.
-
#basename(name, o = {}) ⇒ String+
get a basename of a path.
-
#cd(path = ENV["HOME"], &blk) ⇒ Object
change directory.
-
#dangling?(path) ⇒ Boolean
is path a dangling symlink?.
-
#expand(path) ⇒ String
alias from File.expand_path.
-
#extname(path) ⇒ String
extname of a path.
-
#get(obj) ⇒ String?
get path of an object.
-
#join(*paths) ⇒ String
join paths, skip nil and empty string.
-
#ln(src_s, dest, o = {}) ⇒ nil
(also: #symlink)
link.
-
#ln_f(src_s, dest, o) ⇒ nil
ln force.
-
#parent(path, n = 1) ⇒ String
get parent path.
-
#pwd ⇒ String
print current work directory.
- #readlink(path) ⇒ Object
-
#realpath(path) ⇒ Object
def dsymlink?.
-
#shorten(path) ⇒ String
shorten a path, convert /home/user/file to ~/file.
-
#split(name, o = {}) ⇒ Array<String>
split path.
-
#symln(src_s, dest, o) ⇒ nil
symbol link.
-
#symln_f(src_s, dest, o) ⇒ nil
symln force.
Instance Method Details
#absolute(path) ⇒ String
alias from File.absolute_path
8 |
# File 'lib/tagen/core/pa/path.rb', line 8 def absolute(path); File.absolute_path(get(path)) end |
#absolute?(path) ⇒ Boolean
is path an absolute path ?
66 |
# File 'lib/tagen/core/pa/path.rb', line 66 def absolute?(path) path=get(path); File.absolute_path(path) == path end |
#basename(name, o = {}) ⇒ String+
get a basename of a path
79 80 81 82 83 84 85 86 87 |
# File 'lib/tagen/core/pa/path.rb', line 79 def basename(name, o={}) name = File.basename(get(name)) if o[:ext] name, ext = name.match(NAME_EXT_PAT).captures [ name, (ext || "")] else name end end |
#cd(path = ENV["HOME"], &blk) ⇒ Object
change directory
31 |
# File 'lib/tagen/core/pa/path.rb', line 31 def cd(path=ENV["HOME"], &blk) Dir.chdir(get(path), &blk) end |
#dangling?(path) ⇒ Boolean
is path a dangling symlink?
a dangling symlink is a dead symlink.
198 199 200 201 202 203 204 205 206 |
# File 'lib/tagen/core/pa/path.rb', line 198 def dangling? path path=get(path) if File.symlink?(path) src = File.readlink(path) not File.exists?(src) else nil end end |
#expand(path) ⇒ String
alias from File.expand_path
13 |
# File 'lib/tagen/core/pa/path.rb', line 13 def (path); File.(get(path)) end |
#extname(path) ⇒ String
extname of a path
57 58 59 60 |
# File 'lib/tagen/core/pa/path.rb', line 57 def extname path _, ext = get(path).match(/\.([^.]+)$/).to_a ext end |
#get(obj) ⇒ String?
get path of an object.
return obj#path if object has a ‘path’ instance method
39 40 41 42 43 44 45 46 47 |
# File 'lib/tagen/core/pa/path.rb', line 39 def get obj if obj.respond_to?(:path) obj.path elsif String === obj obj else raise Error, "not support type -- #{obj.inspect}(#{obj.class})" end end |
#join(*paths) ⇒ String
join paths, skip nil and empty string.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/tagen/core/pa/path.rb', line 120 def join *paths paths.map!{|v|get(v)} # skip nil paths.compact! # skip empty string paths.delete("") File.join(*paths) end |
#ln(src, dest) ⇒ nil #ln([src,..], directory) ⇒ nil Also known as: symlink
link
155 |
# File 'lib/tagen/core/pa/path.rb', line 155 def ln(src_s, dest, o={}) _ln(File.method(:link), src_s, dest, o) end |
#ln_f(src_s, dest, o) ⇒ nil
ln force
161 |
# File 'lib/tagen/core/pa/path.rb', line 161 def ln_f(src_s, dest, o) o[:force]=true; _ln(File.method(:link), src_s, dest, o) end |
#parent(path, n = 1) ⇒ String
get parent path
137 138 139 140 141 142 143 |
# File 'lib/tagen/core/pa/path.rb', line 137 def parent path, n=1 path = get(path) n.times do path = File.dirname(path) end path end |
#pwd ⇒ String
print current work directory
26 |
# File 'lib/tagen/core/pa/path.rb', line 26 def pwd() Dir.getwd end |
#readlink(path) ⇒ Object
190 |
# File 'lib/tagen/core/pa/path.rb', line 190 def readlink(path) File.readlink(get(path)) end |
#realpath(path) ⇒ Object
def dsymlink?
208 |
# File 'lib/tagen/core/pa/path.rb', line 208 def realpath(path) File.realpath(get(path)) end |
#shorten(path) ⇒ String
shorten a path, convert /home/user/file to ~/file
20 21 22 |
# File 'lib/tagen/core/pa/path.rb', line 20 def shorten(path); get(path).sub(%r!^#{Regexp.escape(ENV["HOME"])}!, "~") end |
#split(name, o = {}) ⇒ Array<String>
split path
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tagen/core/pa/path.rb', line 100 def split(name, o={}) dir, fname = File.split(get(name)) ret = Array.wrap(basename(fname, o)) if o[:all] loop do dir1, fname = File.split(dir) break if dir1 == dir ret.unshift fname dir = dir1 end end ret.unshift dir ret end |
#symln(src_s, dest, o) ⇒ nil
symbol link
167 |
# File 'lib/tagen/core/pa/path.rb', line 167 def symln(src_s, dest, o) _ln(File.method(:symlink), src_s, dest, o) end |
#symln_f(src_s, dest, o) ⇒ nil
symln force
174 |
# File 'lib/tagen/core/pa/path.rb', line 174 def symln_f(src_s, dest, o) o[:force]=true; _ln(File.method(:symlink), src_s, dest, o) end |