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_METHODS =
[ :build, :join ]
- DELEGATE_METHODS2 =
[ :join2 ]
- DELEGATE_ATTR_METHODS =
[ :dir, :rel, :rea ]
- VERSION =
"1.3.0"
Constants included from State
Instance Attribute Summary collapse
-
#absolute2 ⇒ Object
(also: #a2)
readonly
Returns the value of attribute absolute2.
-
#base2 ⇒ Object
(also: #fname2, #base, #b2)
readonly
Returns the value of attribute base2.
-
#dir2 ⇒ Object
(also: #d2)
readonly
> “.”, “..”, “/”, “c:”.
-
#dir_strict2 ⇒ Object
(also: #d_s2)
readonly
Pa(“foo”) => “” Pa(“./foo”) => “.”.
-
#ext2 ⇒ Object
(also: #ext, #e2)
readonly
> “ogg”, “”.
-
#fext2 ⇒ Object
(also: #fext, #fe2)
readonly
> “.ogg”, “”.
-
#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
-
.build2(*args, &blk) ⇒ Object
build a path options :path, :dir, :fname, :base, :name, :fext, :ext use Pa.join2.
-
.get(obj) ⇒ String?
get path of an object.
-
.join2(*paths) ⇒ String
join paths, skip nil and empty string.
-
.split(*args) ⇒ Object
special case.
-
.split2(name, o = {}) ⇒ Array<String>
split path.
Instance Method Summary collapse
-
#+(str) ⇒ Pa
add string to path.
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #=~(regexp) ⇒ Object
- #absolute ⇒ Object (also: #a)
- #base_dir ⇒ Object
- #build2(data = {}, &blk) ⇒ String
- #dir ⇒ Object (also: #d)
- #dir_strict ⇒ Object (also: #d_s)
- #end_with?(*args) ⇒ Boolean
- #gsub(*args, &blk) ⇒ Pa
- #gsub!(*args, &blk) ⇒ Pa
- #gsub2(*args, &blk) ⇒ String
-
#initialize(path, o = {}) ⇒ Pa
constructor
A new instance of Pa.
-
#inspect ⇒ String
return ‘#<Pa @path=“foo”, @absolute=“/home/foo”>’.
- #match(*args, &blk) ⇒ MatchData
-
#replace(path) ⇒ Pa
The same Pa object.
- #short ⇒ Object
- #start_with?(*args) ⇒ Boolean
- #sub(*args, &blk) ⇒ Pa
- #sub!(*args, &blk) ⇒ Pa
- #sub2(*args, &blk) ⇒ String
-
#to_s ⇒ String
return ‘/home/foo’.
Methods included from Util::Concern
Constructor Details
#initialize(path, o = {}) ⇒ Pa
Returns a new instance of Pa.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/pa.rb', line 219 def initialize(path, o={}) @path2 = Pa.get(path) # convert ~ to ENV["HOME"] @path2.sub!(/^~/, ENV["HOME"].to_s) if @path2 # nil @options = 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.
212 213 214 |
# File 'lib/pa.rb', line 212 def absolute2 @absolute2 end |
#base2 ⇒ Object (readonly) Also known as: fname2, base, b2
Returns the value of attribute base2.
212 213 214 |
# File 'lib/pa.rb', line 212 def base2 @base2 end |
#dir2 ⇒ Object (readonly) Also known as: d2
> “.”, “..”, “/”, “c:”
“foo” => “.” “./foo” => “.” “../../foo” => “../..”
261 262 263 |
# File 'lib/pa.rb', line 261 def dir2 @dir2 end |
#dir_strict2 ⇒ Object (readonly) Also known as: d_s2
Pa(“foo”) => “” Pa(“./foo”) => “.”
271 272 273 |
# File 'lib/pa.rb', line 271 def dir_strict2 @dir_strict2 end |
#ext2 ⇒ Object (readonly) Also known as: ext, e2
> “ogg”, “”
296 297 298 |
# File 'lib/pa.rb', line 296 def ext2 @ext2 end |
#fext2 ⇒ Object (readonly) Also known as: fext, fe2
> “.ogg”, “”
301 302 303 |
# File 'lib/pa.rb', line 301 def fext2 @fext2 end |
#name2 ⇒ Object (readonly) Also known as: name, n2
Returns the value of attribute name2.
212 213 214 |
# File 'lib/pa.rb', line 212 def name2 @name2 end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
213 214 215 |
# File 'lib/pa.rb', line 213 def @options end |
#path2 ⇒ Object (readonly) Also known as: path
Returns the value of attribute path2.
211 212 213 |
# File 'lib/pa.rb', line 211 def path2 @path2 end |
#rea2 ⇒ Object (readonly)
Returns the value of attribute rea2.
212 213 214 |
# File 'lib/pa.rb', line 212 def rea2 @rea2 end |
#rel2 ⇒ Object (readonly)
Returns the value of attribute rel2.
212 213 214 |
# File 'lib/pa.rb', line 212 def rel2 @rel2 end |
#short2 ⇒ Object (readonly)
Returns the value of attribute short2.
212 213 214 |
# File 'lib/pa.rb', line 212 def short2 @short2 end |
Class Method Details
.build2(path) ⇒ Object .build2(data = {}) ⇒ Object .build2(data = {}) ⇒ Object
build a path options :path, :dir, :fname, :base, :name, :fext, :ext use Pa.join2
161 162 163 164 165 166 167 |
# File 'lib/pa.rb', line 161 def build2(*args, &blk) data = Hash===args.last ? args.pop : {} path = args[0] || build_path2(data) blk ||= proc {|pa| pa.p } blk.call(Pa(path)) end |
.get(obj) ⇒ String?
get path of an object.
return obj#path if object has a ‘path’ instance method
nil -> nil
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pa.rb', line 90 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.
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pa.rb', line 139 def join2(*paths) paths.map!{|v|get(v)} # skip nil paths.compact! # skip empty string paths.delete("") File.join(*paths) end |
.split(*args) ⇒ Object
special case
130 131 132 133 |
# File 'lib/pa.rb', line 130 def split(*args) dir, *names = split2(*args) [ Pa(dir), *names] end |
.split2(name, o = {}) ⇒ Array<String>
split path
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pa.rb', line 113 def split2(name, o={}) dir, fname = File.split(get(name)) ret = Util.wrap_array(File.basename(fname)) 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 |
Instance Method Details
#+(str) ⇒ Pa
add string to path
386 387 388 |
# File 'lib/pa.rb', line 386 def +(str) Pa(path+str) end |
#<=>(other) ⇒ Object
370 371 372 |
# File 'lib/pa.rb', line 370 def <=>(other) path <=> Pa.get(other) end |
#==(other) ⇒ Object
361 362 363 364 365 366 367 368 |
# File 'lib/pa.rb', line 361 def ==(other) case other when Pa self.path == other.path else false end end |
#=~(regexp) ⇒ Object
374 375 376 |
# File 'lib/pa.rb', line 374 def =~(regexp) path =~ regexp end |
#absolute ⇒ Object Also known as: a
251 252 253 |
# File 'lib/pa.rb', line 251 def absolute @absolute ||= Pa(absolute2) end |
#base_dir ⇒ Object
235 236 237 |
# File 'lib/pa.rb', line 235 def base_dir @base_dir ||= ([:base_dir] || ".") end |
#build2(data = {}, &blk) ⇒ String
444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/pa.rb', line 444 def build2(data={}, &blk) return Pa.new(blk.call(self)) if blk d = if data[:path] {path: data[:path]} elsif data[:fname] || data[:base] {dir: dir_strict2, fname: data[:fname], base: data[:base]} else {dir: dir_strict2, name: name2, ext: ext2}.merge(data) end Pa.build2(d) end |
#dir ⇒ Object Also known as: d
265 266 267 |
# File 'lib/pa.rb', line 265 def dir @dir ||= Pa(dir2) end |
#dir_strict ⇒ Object Also known as: d_s
283 284 285 |
# File 'lib/pa.rb', line 283 def dir_strict @dir_strict ||= Pa(dir_strict2) end |
#end_with?(*args) ⇒ Boolean
439 440 441 |
# File 'lib/pa.rb', line 439 def end_with?(*args) path.end_with?(*args) end |
#gsub(*args, &blk) ⇒ Pa
414 415 416 |
# File 'lib/pa.rb', line 414 def gsub(*args, &blk) Pa(gsub2(*args, &blk)) end |
#gsub!(*args, &blk) ⇒ Pa
424 425 426 |
# File 'lib/pa.rb', line 424 def gsub!(*args,&blk) self.replace path.gsub(*args,&blk) end |
#gsub2(*args, &blk) ⇒ String
404 405 406 |
# File 'lib/pa.rb', line 404 def gsub2(*args, &blk) path.gsub(*args, &blk) end |
#inspect ⇒ String
return ‘#<Pa @path=“foo”, @absolute=“/home/foo”>’
340 341 342 343 344 345 |
# File 'lib/pa.rb', line 340 def inspect ret="#<" + self.class.to_s + " " ret += "@path=\"#{path}\", @absolute2=\"#{absolute2}\"" ret += " >" ret end |
#match(*args, &blk) ⇒ MatchData
429 430 431 |
# File 'lib/pa.rb', line 429 def match(*args,&blk) path.match(*args,&blk) end |
#replace(path) ⇒ Pa
Returns the same Pa object.
356 357 358 359 |
# File 'lib/pa.rb', line 356 def replace(path) @path2 = Pa.get(path) initialize_variables end |
#short ⇒ Object
394 395 396 |
# File 'lib/pa.rb', line 394 def short @short ||= Pa(short2) end |
#start_with?(*args) ⇒ Boolean
434 435 436 |
# File 'lib/pa.rb', line 434 def start_with?(*args) path.start_with?(*args) end |
#sub(*args, &blk) ⇒ Pa
409 410 411 |
# File 'lib/pa.rb', line 409 def sub(*args, &blk) Pa(sub2(*args, &blk)) end |
#sub!(*args, &blk) ⇒ Pa
419 420 421 |
# File 'lib/pa.rb', line 419 def sub!(*args,&blk) self.replace path.sub(*args,&blk) end |
#sub2(*args, &blk) ⇒ String
399 400 401 |
# File 'lib/pa.rb', line 399 def sub2(*args, &blk) path.sub(*args, &blk) end |
#to_s ⇒ String
return ‘/home/foo’
350 351 352 |
# File 'lib/pa.rb', line 350 def to_s path end |