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

Instance Method Details

#absolute2(path) ⇒ String

alias from File.absolute_path

Parameters:

  • path (String, Pa)

Returns:

  • (String)


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 ?

Parameters:

  • path (String, Pa)

Returns:

  • (Boolean)


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

Examples:

Pa.basename("foo.bar.c", ext: true)  #=> \["foo.bar", "c"]

Parameters:

  • name (String, Pa)
  • o (Hash) (defaults to: {})

    options

Options Hash (o):

  • :ext (Boolean, String) — default: false

    return [name, ext] if true

Returns:

  • (String)

    basename of a path unless o

  • (Array<String>)

    [name, ext] if o.



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.

Parameters:

  • path (String, Pa)

Returns:

  • (Boolean)


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

Parameters:

  • path (String, Pa)

Returns:

  • (String)


112
113
114
# File 'lib/pa/path.rb', line 112

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

#ext2(path) ⇒ String Also known as: ext

ext of a path

Examples:

"a.ogg" => "ogg"
"a" => nil

Parameters:

  • path (String, Pa)

Returns:

  • (String)


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

Parameters:

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

    up level

Returns:

  • (String)


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

#pwd2String

return current work directory

Returns:

  • (String)

    path



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

Parameters:

  • path (String, Pa)

Returns:

  • (String)


121
122
123
# File 'lib/pa/path.rb', line 121

def shorten2(path)
  get(path).sub /^#{Regexp.escape(ENV["HOME"])}/, "~"
end