Class: Pa

Inherits:
Object
  • Object
show all
Includes:
Cmd, Directory, Path, State
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

State::FILE_DELEGATED_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Concern

#included

Constructor Details

#initialize(path, o = {}) ⇒ Pa

Returns a new instance of Pa.

Parameters:

  • path (String, #path)


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

#absolute2Object (readonly) Also known as: a2

Returns the value of attribute absolute2.



212
213
214
# File 'lib/pa.rb', line 212

def absolute2
  @absolute2
end

#base2Object (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

#dir2Object (readonly) Also known as: d2

> “.”, “..”, “/”, “c:”

“foo” => “.” “./foo” => “.” “../../foo” => “../..”



261
262
263
# File 'lib/pa.rb', line 261

def dir2
  @dir2
end

#dir_strict2Object (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

#ext2Object (readonly) Also known as: ext, e2

> “ogg”, “”



296
297
298
# File 'lib/pa.rb', line 296

def ext2
  @ext2
end

#fext2Object (readonly) Also known as: fext, fe2

> “.ogg”, “”



301
302
303
# File 'lib/pa.rb', line 301

def fext2
  @fext2
end

#name2Object (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

#optionsObject (readonly)

Returns the value of attribute options.



213
214
215
# File 'lib/pa.rb', line 213

def options
  @options
end

#path2Object (readonly) Also known as: path

Returns the value of attribute path2.



211
212
213
# File 'lib/pa.rb', line 211

def path2
  @path2
end

#rea2Object (readonly)

Returns the value of attribute rea2.



212
213
214
# File 'lib/pa.rb', line 212

def rea2
  @rea2
end

#rel2Object (readonly)

Returns the value of attribute rel2.



212
213
214
# File 'lib/pa.rb', line 212

def rel2
  @rel2
end

#short2Object (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

Examples:


Pa.build2(dir: "/home", name: "guten", ext: "avi") => "/home/guten.avi
Pa.build2("/home/guten.avi") { |pa| "#{pa.dir}/foo.#{pa.ext}" } => "/home/foo.avi


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

Parameters:

  • obj (String, #path)

Returns:

  • (String, nil)

    path



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.

Parameters:

  • *paths (*Array<String>)

Returns:

  • (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

Examples:

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

Parameters:

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

    option

Options Hash (o):

  • :all (Boolean)

    split all parts

Returns:

  • (Array<String>)


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

Examples:

pa = Pa('/home/foo/a.txt')
pa+'~' #=> new Pa('/home/foo/a.txt~')

Parameters:

  • str (String)

Returns:



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

#absoluteObject Also known as: a



251
252
253
# File 'lib/pa.rb', line 251

def absolute
  @absolute ||= Pa(absolute2)
end

#base_dirObject



235
236
237
# File 'lib/pa.rb', line 235

def base_dir
  @base_dir ||= (options[:base_dir] || ".")
end

#build2(data = {}, &blk) ⇒ String

Returns:

  • (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

#dirObject Also known as: d



265
266
267
# File 'lib/pa.rb', line 265

def dir
  @dir ||= Pa(dir2)
end

#dir_strictObject 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

Returns:

  • (Boolean)


439
440
441
# File 'lib/pa.rb', line 439

def end_with?(*args)
  path.end_with?(*args)
end

#gsub(*args, &blk) ⇒ Pa

Returns:



414
415
416
# File 'lib/pa.rb', line 414

def gsub(*args, &blk)
  Pa(gsub2(*args, &blk))
end

#gsub!(*args, &blk) ⇒ Pa

Returns:



424
425
426
# File 'lib/pa.rb', line 424

def gsub!(*args,&blk)
  self.replace path.gsub(*args,&blk)
end

#gsub2(*args, &blk) ⇒ String

Returns:

  • (String)


404
405
406
# File 'lib/pa.rb', line 404

def gsub2(*args, &blk)
  path.gsub(*args, &blk)
end

#inspectString

return ‘#<Pa @path=“foo”, @absolute=“/home/foo”>’

Returns:

  • (String)


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

Returns:

  • (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.

Parameters:

Returns:

  • (Pa)

    the same Pa object



356
357
358
359
# File 'lib/pa.rb', line 356

def replace(path)
	@path2 = Pa.get(path)
	initialize_variables
end

#shortObject



394
395
396
# File 'lib/pa.rb', line 394

def short
  @short ||= Pa(short2)
end

#start_with?(*args) ⇒ Boolean

Returns:

  • (Boolean)


434
435
436
# File 'lib/pa.rb', line 434

def start_with?(*args)
  path.start_with?(*args)
end

#sub(*args, &blk) ⇒ Pa

Returns:



409
410
411
# File 'lib/pa.rb', line 409

def sub(*args, &blk)
  Pa(sub2(*args, &blk))
end

#sub!(*args, &blk) ⇒ Pa

Returns:



419
420
421
# File 'lib/pa.rb', line 419

def sub!(*args,&blk)
  self.replace path.sub(*args,&blk)
end

#sub2(*args, &blk) ⇒ String

Returns:

  • (String)


399
400
401
# File 'lib/pa.rb', line 399

def sub2(*args, &blk)
  path.sub(*args, &blk)
end

#to_sString

return ‘/home/foo’

Returns:

  • (String)

    path



350
351
352
# File 'lib/pa.rb', line 350

def to_s
	path
end