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]
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

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)


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
  @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.



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

def absolute2
  @absolute2
end

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

#dir2Object (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_strict2Object (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

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

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

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

#optionsObject (readonly)

Returns the value of attribute options.



263
264
265
# File 'lib/pa.rb', line 263

def options
  @options
end

#path2Object (readonly) Also known as: path

Returns the value of attribute path2.



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

def path2
  @path2
end

#rea2Object (readonly)

Returns the value of attribute rea2.



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

def rea2
  @rea2
end

#rel2Object (readonly)

Returns the value of attribute rel2.



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

def rel2
  @rel2
end

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

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



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

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

.join2(*paths) ⇒ String

join paths, skip nil and empty string.

Examples:


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

Parameters:

  • *paths (*Array<String>)

Returns:

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

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>)


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

Examples:

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

Parameters:

  • str (String)

Returns:



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_dirObject



333
334
335
# File 'lib/pa.rb', line 333

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



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

Returns:



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

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

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

Returns:



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

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

#inspectString

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

Returns:

  • (String)


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.

Parameters:

Returns:

  • (Pa)

    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

#shortObject



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

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

#sub(*args, &blk) ⇒ Pa

Returns:



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

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

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

Returns:



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

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

#to_sString

return ‘/home/foo’

Returns:

  • (String)

    path



389
390
391
# File 'lib/pa.rb', line 389

def to_s
	path
end