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)


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

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"


144
145
146
147
148
149
150
151
152
# File 'lib/pa/path.rb', line 144

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)


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.

Examples:


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


126
127
128
129
130
131
132
133
134
# File 'lib/pa/path.rb', line 126

def delete_ext2(path, ext)
  p = get(path)

  if has_ext?(p, ext)
    p[0...p.rindex(ext)]
  else
    p
  end
end

#expand2(path) ⇒ String

Alias from File.expand_path

Parameters:

  • path (String, Pa)

Returns:

  • (String)


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

def expand2(path) 
  File.expand_path get(path) 
end

#has_ext?(path, ext) ⇒ Boolean

Return true if a path has the ext.

Examples:


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

Returns:

  • (Boolean)


114
115
116
# File 'lib/pa/path.rb', line 114

def has_ext?(path, ext)
  Pa.ext2(get(path)) == ext
end

#parent2(path, n = 1) ⇒ String

get parent path

Parameters:

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

    up level

Returns:

  • (String)


185
186
187
188
189
190
191
# File 'lib/pa/path.rb', line 185

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



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

def pwd2
  Dir.getwd 
end

#real2(path) ⇒ Object

real path



176
177
178
# File 'lib/pa/path.rb', line 176

def real2(path) 
  File.realpath get(path)
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)


94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pa/path.rb', line 94

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)


71
72
73
74
75
76
77
78
79
80
# File 'lib/pa/path.rb', line 71

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)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/pa/path.rb', line 159

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