Module: Pa::Path::ClassMethods

Defined in:
lib/pa/path.rb

Instance Method Summary collapse

Instance Method Details

#absolute?(path) ⇒ Boolean

Is path an absolute path ?

Parameters:

  • path (String, Pa)

Returns:

  • (Boolean)


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

Examples:


Pa.add_ext2("foo", ".txt")         -> "foo.txt"
Pa.add_ext2("foo.txt", ".txt")     -> "foo.txt"
Pa.add_ext2("foo.epub", ".txt")    -> "foo.txt.epub"


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.

Parameters:

  • path (String, Pa)

Returns:

  • (Boolean)


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.

Examples:


Pa.delete_ext2("foo.txt", ".epub", ".txt")      -> "foo"
Pa.delete_ext2("foo", ".txt")          -> "foo"
Pa.delete_ext2("foo.epub", ".txt")     -> "foo.epub"


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

Parameters:

  • path (String, Pa)

Returns:

  • (String)


59
60
61
# File 'lib/pa/path.rb', line 59

def expand2(name, dir=".") 
  File.expand_path(get(name), dir) 
end

#has_ext?(path, *exts) ⇒ Boolean

Return true if a path has the ext.

Examples:


Pa.has_ext?("foo.txt", ".epub", ".txt")   -> true
Pa.has_ext?("foo", ".txt")       -> false

Returns:

  • (Boolean)


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

Parameters:

  • path (String, Pa)
  • n (Fixnum) (defaults to: 1)

    up level

Returns:

  • (String)


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

#pwd2String

Return current work directory

Returns:

  • (String)

    path



25
26
27
# File 'lib/pa/path.rb', line 25

def pwd2
  Dir.getwd 
end

#real2(name, dir = ".") ⇒ String

Alias from Filel.realpath

Parameters:

  • path (String, Pa)

Returns:

  • (String)


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.

Examples:


Pa.relative_to2("/home/foo", "/home") -> "foo"
Pa.relative_to2("/home/foo", "/home/foo") -> "."
Pa.relative_to2("/home/foo", "/bin") -> "/home/foo"

Pa.relative_to2("/home/foo", "/home/foo/") -> "."
Pa.relative_to2("/home/foo/", "/home/foo") -> "."

Returns:

  • (String)


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

Examples:


Pa.relative_to?("/home/foo", "/home")  -> true
Pa.relative_to?("/home1/foo", "/home")  -> false

Returns:

  • (Boolean)


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

Parameters:

  • path (String, Pa)

Returns:

  • (String)


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