Class: Pa
- Inherits:
-
Object
- Object
- Pa
- Defined in:
- lib/pa.rb,
lib/pa.rb,
lib/pa/cmd.rb,
lib/pa/path.rb,
lib/pa/util.rb,
lib/pa/state.rb,
lib/pa/version.rb,
lib/pa/directory.rb
Overview
ls family
* Dir _support globbing_ * Pa.glob(*path,o),(){} _support globbing with option and block_ * each(path),(){} each_r(),(){} _support Enumerator. not support globbing_ * ls(path) ls_r(path) _sample ls. not support globbing._
Example
tmp/ filea dira/fileb
ls2(“tmp”) => [“filea”, “dira”] ls2_r(“tmp”) => [“filea”, “dira”, “dira/fileb”] ls2_r(“tmp”){|path, rel| rel.count(‘/’)==1} => [“dira/fileb”]
each(“tmp”) => Enumerate<Pa> each(“tmp”) {|pa| Pa.rm pa if pa.file?} each(“tmp”).with_object([]){|pa,m| m<<pa.dir} #=> [“tmp”, “tmp/dira”]
Defined Under Namespace
Modules: Cmd, Directory, Path, State Classes: Util
Constant Summary collapse
- Error =
Class.new Exception
- EUnkonwType =
Class.new Error
- DELEGATE_CLASS_METHODS =
[:absolute, :dir, :dir_stict, :name, :ext, :fext]
- DELEGATE_ATTR_METHODS2 =
[ :dir2, :dir_strict2, :base2, :name2, :ext2, :fext2]
- DELEGATE_ATTR_METHODS =
[ :absolute, :dir, :dir_strict, :rel, :rea ]
- DELEGATE_METHODS2 =
[ :join2 ]
- DELEGATE_METHODS =
[ :change, :join]
- DELEGATE_TO_PATH2 =
[ :sub2, :gsub2 ]
- DELEGATE_TO_PATH =
[:match, :start_with?, :end_with?]
- VERSION =
"1.3.1"
Constants included from State
Instance Attribute Summary collapse
-
#absolute2 ⇒ Object
(also: #a2)
readonly
Returns the value of attribute absolute2.
-
#base2 ⇒ Object
(also: #base, #b2)
readonly
Returns the value of attribute base2.
-
#dir2 ⇒ Object
(also: #d2)
readonly
Returns the value of attribute dir2.
-
#dir_strict2 ⇒ Object
(also: #d_s2)
readonly
Returns the value of attribute dir_strict2.
-
#ext2 ⇒ Object
(also: #ext, #e2)
readonly
Returns the value of attribute ext2.
-
#fext2 ⇒ Object
(also: #fext, #fe2)
readonly
Returns the value of attribute fext2.
-
#name2 ⇒ Object
(also: #name, #n2)
readonly
Returns the value of attribute name2.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path2 ⇒ Object
(also: #path)
readonly
Returns the value of attribute path2.
-
#rea2 ⇒ Object
readonly
Returns the value of attribute rea2.
-
#rel2 ⇒ Object
readonly
Returns the value of attribute rel2.
-
#short2 ⇒ Object
readonly
Returns the value of attribute short2.
Class Method Summary collapse
- .absolute2(path) ⇒ Object
- .base(*args, &blk) ⇒ Object
-
.base2(name, o = {}) ⇒ String+
get a basename of a path.
-
.dir2(path) ⇒ Object
> “.”, “..”, “/”, “c:”.
-
.dir_strict2(path) ⇒ Object
Pa(“foo”) => “” Pa(“./foo”) => “.”.
-
.ext2(path) ⇒ Object
-> “.ogg”, “”.
-
.fext2(path) ⇒ Object
> “ogg”, “”.
-
.get(obj) ⇒ String?
get path of an object.
-
.join2(*paths) ⇒ String
join paths, skip nil and empty string.
- .name2(path) ⇒ Object
-
.split(*args) ⇒ Object
special case.
-
.split2(path, o = {}) ⇒ Array<String>
split path.
Instance Method Summary collapse
-
#+(str) ⇒ Pa
add string to path.
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #=~(regexp) ⇒ Object
- #base_dir ⇒ Object
-
#change2(data = {}, &blk) ⇒ String
Change some parts of the path.
- #gsub(*args, &blk) ⇒ Pa
- #gsub!(*args, &blk) ⇒ Pa
-
#initialize(path, o = {}) ⇒ Pa
constructor
A new instance of Pa.
-
#inspect ⇒ String
return ‘#<Pa @path=“foo”, @absolute=“/home/foo”>’.
-
#replace(path) ⇒ Pa
The same Pa object.
- #short ⇒ Object
- #sub(*args, &blk) ⇒ Pa
- #sub!(*args, &blk) ⇒ Pa
-
#to_s ⇒ String
return ‘/home/foo’.
Methods included from Util::Concern
Methods included from State
#chmod, #chown, #lchmod, #lchown, #utime
Constructor Details
#initialize(path, o = {}) ⇒ Pa
Returns a new instance of Pa.
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/pa.rb', line 269 def initialize(path, o={}) @path2 = Pa.get(path) # convert ~ to ENV["HOME"] @path2.sub!(/^~/, ENV["HOME"].to_s) if @path2 # nil = o @base_dir = o[:base_dir] || "." initialize_variables end |
Instance Attribute Details
#absolute2 ⇒ Object (readonly) Also known as: a2
Returns the value of attribute absolute2.
262 263 264 |
# File 'lib/pa.rb', line 262 def absolute2 @absolute2 end |
#base2 ⇒ Object (readonly) Also known as: base, b2
Returns the value of attribute base2.
262 263 264 |
# File 'lib/pa.rb', line 262 def base2 @base2 end |
#dir2 ⇒ Object (readonly) Also known as: d2
Returns the value of attribute dir2.
262 263 264 |
# File 'lib/pa.rb', line 262 def dir2 @dir2 end |
#dir_strict2 ⇒ Object (readonly) Also known as: d_s2
Returns the value of attribute dir_strict2.
262 263 264 |
# File 'lib/pa.rb', line 262 def dir_strict2 @dir_strict2 end |
#ext2 ⇒ Object (readonly) Also known as: ext, e2
Returns the value of attribute ext2.
262 263 264 |
# File 'lib/pa.rb', line 262 def ext2 @ext2 end |
#fext2 ⇒ Object (readonly) Also known as: fext, fe2
Returns the value of attribute fext2.
262 263 264 |
# File 'lib/pa.rb', line 262 def fext2 @fext2 end |
#name2 ⇒ Object (readonly) Also known as: name, n2
Returns the value of attribute name2.
262 263 264 |
# File 'lib/pa.rb', line 262 def name2 @name2 end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
263 264 265 |
# File 'lib/pa.rb', line 263 def end |
#path2 ⇒ Object (readonly) Also known as: path
Returns the value of attribute path2.
261 262 263 |
# File 'lib/pa.rb', line 261 def path2 @path2 end |
#rea2 ⇒ Object (readonly)
Returns the value of attribute rea2.
262 263 264 |
# File 'lib/pa.rb', line 262 def rea2 @rea2 end |
#rel2 ⇒ Object (readonly)
Returns the value of attribute rel2.
262 263 264 |
# File 'lib/pa.rb', line 262 def rel2 @rel2 end |
#short2 ⇒ Object (readonly)
Returns the value of attribute short2.
262 263 264 |
# File 'lib/pa.rb', line 262 def short2 @short2 end |
Class Method Details
.absolute2(path) ⇒ Object
109 110 111 |
# File 'lib/pa.rb', line 109 def absolute2(path) File.absolute_path(get(path), ".") # rbx end |
.base(*args, &blk) ⇒ Object
156 157 158 159 160 161 162 163 164 |
# File 'lib/pa.rb', line 156 def base(*args, &blk) ret = base2(*args, &blk) if Array === ret [ Pa(ret[0]), ret[1] ] else Pa(ret) end end |
.base2(name, o = {}) ⇒ String+
get a basename of a path
146 147 148 149 150 151 152 153 154 |
# File 'lib/pa.rb', line 146 def base2(name, o={}) name = File.basename(get(name)) if o[:ext] name, ext = name.match(/^(.+?)(?:\.([^.]+))?$/).captures [ name, (ext || "")] else name end end |
.dir2(path) ⇒ Object
> “.”, “..”, “/”, “c:”
“foo” => “.” “./foo” => “.” “../../foo” => “../..”
119 120 121 |
# File 'lib/pa.rb', line 119 def dir2(path) File.dirname(get(path)) end |
.dir_strict2(path) ⇒ Object
Pa(“foo”) => “” Pa(“./foo”) => “.”
125 126 127 128 129 130 131 132 133 |
# File 'lib/pa.rb', line 125 def dir_strict2(path) dir = File.dirname(get(path)) if %w[.].include?(dir) && path !~ %r~^\./~ "" else dir end end |
.ext2(path) ⇒ Object
-> “.ogg”, “”
171 172 173 |
# File 'lib/pa.rb', line 171 def ext2(path) File.extname(get(path)) end |
.fext2(path) ⇒ Object
> “ogg”, “”
176 177 178 |
# File 'lib/pa.rb', line 176 def fext2(path) File.extname(get(path)).gsub(/^\./, "") end |
.get(obj) ⇒ String?
get path of an object.
return obj#path if object has a ‘path’ instance method
nil -> nil
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pa.rb', line 97 def get(obj) if String === obj obj elsif obj.respond_to?(:path) obj.path elsif obj.nil? nil else raise ArgumentError, "Pa.get() not support type -- #{obj.inspect}(#{obj.class})" end end |
.join2(*paths) ⇒ String
join paths, skip nil and empty string.
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/pa.rb', line 222 def join2(*paths) paths.map!{|v|get(v)} # skip nil paths.compact! # skip empty string paths.delete("") File.join(*paths) end |
.name2(path) ⇒ Object
166 167 168 |
# File 'lib/pa.rb', line 166 def name2(path) File.basename(get(path)).match(/^(.+?)(?:\.([^.]+))?$/)[1] end |
.split(*args) ⇒ Object
special case
209 210 211 212 |
# File 'lib/pa.rb', line 209 def split(*args) dir, *names = split2(*args) [ Pa(dir), *names] end |
.split2(path, o = {}) ⇒ Array<String>
split path
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/pa.rb', line 192 def split2(path, o={}) dir, base = File.split(get(path)) ret = Util.wrap_array(File.basename(base)) if o[:all] loop do dir1, base = File.split(dir) break if dir1 == dir ret.unshift base dir = dir1 end end ret.unshift dir ret end |
Instance Method Details
#+(str) ⇒ Pa
add string to path
426 427 428 |
# File 'lib/pa.rb', line 426 def +(str) Pa(path+str) end |
#<=>(other) ⇒ Object
410 411 412 |
# File 'lib/pa.rb', line 410 def <=>(other) path <=> Pa.get(other) end |
#==(other) ⇒ Object
401 402 403 404 405 406 407 408 |
# File 'lib/pa.rb', line 401 def ==(other) case other when Pa self.path == other.path else false end end |
#=~(regexp) ⇒ Object
414 415 416 |
# File 'lib/pa.rb', line 414 def =~(regexp) path =~ regexp end |
#base_dir ⇒ Object
333 334 335 |
# File 'lib/pa.rb', line 333 def base_dir @base_dir ||= ([:base_dir] || ".") end |
#change2(data = {}, &blk) ⇒ String
Change some parts of the path.
path dir base dir name ext …
466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/pa.rb', line 466 def change2(data={}, &blk) return Pa.new(blk.call(self)) if blk if data[:path] return data[:path] elsif data[:base] return File.join(data[:dir] || dir2, data[:base]) else dir, name, ext = data[:dir] || dir2, data[:name] || name2, data[:ext] || ext2 File.join(dir, name)+ext end end |
#gsub(*args, &blk) ⇒ Pa
444 445 446 |
# File 'lib/pa.rb', line 444 def gsub(*args, &blk) Pa(gsub2(*args, &blk)) end |
#gsub!(*args, &blk) ⇒ Pa
454 455 456 |
# File 'lib/pa.rb', line 454 def gsub!(*args,&blk) replace path.gsub(*args,&blk) end |
#inspect ⇒ String
return ‘#<Pa @path=“foo”, @absolute=“/home/foo”>’
379 380 381 382 383 384 |
# File 'lib/pa.rb', line 379 def inspect ret="#<" + self.class.to_s + " " ret += "@path=\"#{path}\", @absolute2=\"#{absolute2}\"" ret += " >" ret end |
#replace(path) ⇒ Pa
Returns the same Pa object.
395 396 397 398 399 |
# File 'lib/pa.rb', line 395 def replace(path) @path2 = Pa.get(path) initialize_variables end |
#short ⇒ Object
434 435 436 |
# File 'lib/pa.rb', line 434 def short @short ||= Pa(short2) end |
#sub(*args, &blk) ⇒ Pa
439 440 441 |
# File 'lib/pa.rb', line 439 def sub(*args, &blk) Pa(sub2(*args, &blk)) end |
#sub!(*args, &blk) ⇒ Pa
449 450 451 |
# File 'lib/pa.rb', line 449 def sub!(*args,&blk) replace path.sub(*args,&blk) end |
#to_s ⇒ String
return ‘/home/foo’
389 390 391 |
# File 'lib/pa.rb', line 389 def to_s path end |