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_CLASS_METHODS =
[:absolute, :dir, :dir_stict, :name, :ext, :fext]
DELEGATE_ATTR_METHODS2 =
[ :dir2, :dir_strict2, :base2, :name2, :ext2, :fext2, :head2]
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.4.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

Methods included from State

#chmod, #chown, #lchmod, #lchown, #utime

Constructor Details

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

Returns a new instance of Pa.

Parameters:

  • path (String, #path)


280
281
282
283
284
285
286
287
288
289
# File 'lib/pa.rb', line 280

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.



273
274
275
# File 'lib/pa.rb', line 273

def absolute2
  @absolute2
end

#base2Object (readonly) Also known as: base, b2

Returns the value of attribute base2.



273
274
275
# File 'lib/pa.rb', line 273

def base2
  @base2
end

#dir2Object (readonly) Also known as: d2

Returns the value of attribute dir2.



273
274
275
# File 'lib/pa.rb', line 273

def dir2
  @dir2
end

#dir_strict2Object (readonly) Also known as: d_s2

Returns the value of attribute dir_strict2.



273
274
275
# File 'lib/pa.rb', line 273

def dir_strict2
  @dir_strict2
end

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

Returns the value of attribute ext2.



273
274
275
# File 'lib/pa.rb', line 273

def ext2
  @ext2
end

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

Returns the value of attribute fext2.



273
274
275
# File 'lib/pa.rb', line 273

def fext2
  @fext2
end

#head2Object (readonly)

Returns the value of attribute head2.



273
274
275
# File 'lib/pa.rb', line 273

def head2
  @head2
end

#name2Object (readonly) Also known as: name, n2

Returns the value of attribute name2.



273
274
275
# File 'lib/pa.rb', line 273

def name2
  @name2
end

#optionsObject (readonly)

Returns the value of attribute options.



274
275
276
# File 'lib/pa.rb', line 274

def options
  @options
end

#path2Object (readonly) Also known as: path

Returns the value of attribute path2.



272
273
274
# File 'lib/pa.rb', line 272

def path2
  @path2
end

#rea2Object (readonly)

Returns the value of attribute rea2.



273
274
275
# File 'lib/pa.rb', line 273

def rea2
  @rea2
end

#rel2Object (readonly)

Returns the value of attribute rel2.



273
274
275
# File 'lib/pa.rb', line 273

def rel2
  @rel2
end

#short2Object (readonly)

Returns the value of attribute short2.



273
274
275
# File 'lib/pa.rb', line 273

def short2
  @short2
end

Class Method Details

.absolute2(name, dir = ".") ⇒ Object



109
110
111
# File 'lib/pa.rb', line 109

def absolute2(name, dir=".")
  File.absolute_path(get(name), dir)
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

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.



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”, “”



187
188
189
# File 'lib/pa.rb', line 187

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

Parameters:

  • obj (String, #path)

Returns:

  • (String, nil)

    path



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

.head2(path) ⇒ Object

Return path without ext.

Pa.head2("/foo/a.txt") -> "/foo/a")


179
180
181
182
183
184
# File 'lib/pa.rb', line 179

def head2(path)
  p = get(path)
  ext = File.extname(p)

  ext.empty? ? p : p[0...-ext.length]
end

.join2(*paths) ⇒ String

join paths, skip nil and empty string.

Examples:


Pa.join2("", "foo", nil, "bar")  -> "foo/bar"

Parameters:

  • *paths (*Array<String>)

Returns:

  • (String)


233
234
235
236
237
238
239
240
241
242
# File 'lib/pa.rb', line 233

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



220
221
222
223
# File 'lib/pa.rb', line 220

def split(*args)
  dir, *names = split2(*args)
  [ Pa(dir), *names]
end

.split2(path, o = {}) ⇒ Array<String>

split path

Examples:


path="/home/a/file"
split2(path)                -> ["/home/a", "file"]
split2(path, :all => true)  -> ["/", "home", "a", "file"]

Parameters:

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

    option

Options Hash (o):

  • :all (Boolean)

    split all parts

Returns:

  • (Array<String>)


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/pa.rb', line 203

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

Examples:

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

Parameters:

  • str (String)

Returns:



437
438
439
# File 'lib/pa.rb', line 437

def +(str)
  Pa(path+str)
end

#<=>(other) ⇒ Object



421
422
423
# File 'lib/pa.rb', line 421

def <=>(other)
   path <=> Pa.get(other)
end

#==(other) ⇒ Object



412
413
414
415
416
417
418
419
# File 'lib/pa.rb', line 412

def ==(other)
  case other
  when Pa
    self.path == other.path
  else
    false
  end
end

#=~(regexp) ⇒ Object



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

def =~(regexp)
  path =~ regexp 
end

#base_dirObject



344
345
346
# File 'lib/pa.rb', line 344

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

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

Change some parts of the path.

path dir base dir name ext …

Returns:

  • (String)

    path



477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/pa.rb', line 477

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

Returns:



455
456
457
# File 'lib/pa.rb', line 455

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

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

Returns:



465
466
467
# File 'lib/pa.rb', line 465

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

#inspectString

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

Returns:

  • (String)


390
391
392
393
394
395
# File 'lib/pa.rb', line 390

def inspect
	ret="#<" + self.class.to_s + " "
	ret += "@path=\"#{path}\", @absolute2=\"#{absolute2}\""
	ret += " >"
	ret
end

#replace(path) ⇒ Pa

Returns the same Pa object.

Parameters:

Returns:

  • (Pa)

    the same Pa object



406
407
408
409
410
# File 'lib/pa.rb', line 406

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

	initialize_variables
end

#shortObject



445
446
447
# File 'lib/pa.rb', line 445

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

#sub(*args, &blk) ⇒ Pa

Returns:



450
451
452
# File 'lib/pa.rb', line 450

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

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

Returns:



460
461
462
# File 'lib/pa.rb', line 460

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

#to_sString

return ‘/home/foo’

Returns:

  • (String)

    path



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

def to_s
	path
end