Module: Pa::Path::ClassMethods
- Defined in:
- lib/pa/path.rb
Constant Summary collapse
- DELEGATE_METHODS =
[:pwd, :dir, :absolute, :expand, :real, :parent]
Instance Method Summary collapse
-
#absolute2(path) ⇒ String
alias from File.absolute_path.
-
#absolute?(path) ⇒ Boolean
is path an absolute path ?.
- #base(*args, &blk) ⇒ Object
-
#base2(name, o = {}) ⇒ String+
get a basename of a path.
-
#dangling?(path) ⇒ Boolean
is path a dangling symlink?.
-
#dir2(path) ⇒ Object
def dsymlink?.
-
#expand2(path) ⇒ String
alias from File.expand_path.
-
#ext2(path) ⇒ String
(also: #ext)
ext of a path.
-
#parent2(path, n = 1) ⇒ String
get parent path.
-
#pwd2 ⇒ String
return current work directory.
-
#real2(path) ⇒ Object
real path.
-
#shorten2(path) ⇒ String
(also: #shorten)
shorten2 a path, convert /home/user/file to ~/file.
Instance Method Details
#absolute2(path) ⇒ String
alias from File.absolute_path
105 106 107 |
# File 'lib/pa/path.rb', line 105 def absolute2(path) File.absolute_path get(path) end |
#absolute?(path) ⇒ Boolean
is path an absolute path ?
31 32 33 34 |
# File 'lib/pa/path.rb', line 31 def absolute?(path) path=get(path) File.absolute_path(path) == path end |
#base(*args, &blk) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/pa/path.rb', line 77 def base(*args, &blk) rst = base2(*args, &blk) if Array===rst [ Pa(rst[0]), rst[1] ] else rst end end |
#base2(name, o = {}) ⇒ String+
get a basename of a path
67 68 69 70 71 72 73 74 75 |
# File 'lib/pa/path.rb', line 67 def base2(name, o={}) name = File.basename(get(name)) if o[:ext] name, ext = name.match(/^(.+?)(?:\.([^.]+))?$/).captures [ name, (ext || "")] else name end end |
#dangling?(path) ⇒ Boolean
is path a dangling symlink?
a dangling symlink is a dead symlink.
42 43 44 45 46 47 48 49 50 |
# File 'lib/pa/path.rb', line 42 def dangling? path path=get(path) if File.symlink?(path) src = File.readlink(path) not File.exists?(src) else nil end end |
#dir2(path) ⇒ Object
def dsymlink?
52 53 54 |
# File 'lib/pa/path.rb', line 52 def dir2(path) File.dirname(path) end |
#expand2(path) ⇒ String
alias from File.expand_path
112 113 114 |
# File 'lib/pa/path.rb', line 112 def (path) File. get(path) end |
#ext2(path) ⇒ String Also known as: ext
ext of a path
95 96 97 98 |
# File 'lib/pa/path.rb', line 95 def ext2 path _, ext = get(path).match(/\.([^.]+)$/).to_a ext end |
#parent2(path, n = 1) ⇒ String
get parent path
137 138 139 140 141 142 143 |
# File 'lib/pa/path.rb', line 137 def parent2(path, n=1) path = get(path) n.times do path = File.dirname(path) end path end |
#pwd2 ⇒ String
return current work directory
23 24 25 |
# File 'lib/pa/path.rb', line 23 def pwd2 Dir.getwd end |
#real2(path) ⇒ Object
real path
128 129 130 |
# File 'lib/pa/path.rb', line 128 def real2(path) File.realpath get(path) end |
#shorten2(path) ⇒ String Also known as: shorten
shorten2 a path, convert /home/user/file to ~/file
121 122 123 |
# File 'lib/pa/path.rb', line 121 def shorten2(path) get(path).sub /^#{Regexp.escape(ENV["HOME"])}/, "~" end |