Module: Pa::ClassMethods::Path

Included in:
Pa
Defined in:
lib/tagen/core/pa/path.rb

Instance Method Summary collapse

Instance Method Details

#absolute(path) ⇒ String

alias from File.absolute_path

Parameters:

Returns:



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 ?

Parameters:

Returns:

  • (Boolean)


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

Examples:

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

Parameters:

Options Hash (o):

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

    return [name, ext] if true

Returns:



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

Parameters:

  • path (String, Pa) (defaults to: ENV["HOME"])


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.

Parameters:

Returns:

  • (Boolean)


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

Parameters:

Returns:



13
# File 'lib/tagen/core/pa/path.rb', line 13

def expand(path); File.expand_path(get(path)) end

#extname(path) ⇒ String

extname of a path

Examples:

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

Parameters:

Returns:



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

Parameters:

Returns:



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.

Parameters:

Returns:



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

Parameters:

Options Hash (o):

  • :force (Boolean)

    overwrite if exists.

Returns:

  • (nil)


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

Returns:

  • (nil)

See Also:



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

Parameters:

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

    up level

Returns:



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

#pwdString

print current work directory

Returns:



26
# File 'lib/tagen/core/pa/path.rb', line 26

def pwd() Dir.getwd end

See Also:

  • File.readlink


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

Parameters:

Returns:



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

Examples:

path="/home/a/file"
split(path)  #=> "/home/a", "file"
split(path, :all)  #=> "/", "home", "a", "file"

Parameters:

Options Hash (o):

  • :all (Boolean)

    split all parts

Returns:



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

Returns:

  • (nil)

See Also:



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

Returns:

  • (nil)

See Also:



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