Class: FSSM::Pathname
- Inherits:
-
String
- Object
- String
- FSSM::Pathname
- Defined in:
- lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb,
lib/fssm/pathname.rb
Constant Summary collapse
- SEPARATOR =
Regexp.quote(File::SEPARATOR)
- ALT_SEPARATOR =
Regexp.quote(File::ALT_SEPARATOR)
- SEPARATOR_PAT =
Regexp.compile(SEPARATOR)
- PREFIX_PAT =
Regexp.compile("^(#{SEPARATOR_PAT})")
Class Method Summary collapse
- .[](pattern) ⇒ Object
- .for(path) ⇒ Object
- .glob(pattern, flags = 0) ⇒ Object
- .join(*parts) ⇒ Object
- .pwd ⇒ Object
Instance Method Summary collapse
- #+(path) ⇒ Object
- #<<(path) ⇒ Object
- #absolute? ⇒ Boolean
- #ascend ⇒ Object
- #atime ⇒ Object
- #basename ⇒ Object
- #blockdev? ⇒ Boolean
- #chardev? ⇒ Boolean
- #chdir ⇒ Object
- #chmod(mode) ⇒ Object
- #chown(owner, group) ⇒ Object
- #cleanpath ⇒ Object
- #cleanpath! ⇒ Object
- #ctime ⇒ Object
- #dememo ⇒ Object
- #descend ⇒ Object
- #directory? ⇒ Boolean
- #dirname ⇒ Object
- #each_filename(&block) ⇒ Object
- #each_line(sep = $/, &blk) ⇒ Object
- #entries ⇒ Object
- #executable? ⇒ Boolean
- #executable_real? ⇒ Boolean
- #exists? ⇒ Boolean (also: #exist?)
- #expand_path(from = nil) ⇒ Object
- #extname ⇒ Object
- #file? ⇒ Boolean
- #find ⇒ Object
- #fnmatch?(pat, flags = 0) ⇒ Boolean
- #ftype ⇒ Object
- #grpowned? ⇒ Boolean
-
#initialize(path) ⇒ Pathname
constructor
A new instance of Pathname.
- #join(*parts) ⇒ Object
- #lchmod(mode) ⇒ Object
- #lchown(owner, group) ⇒ Object
- #link(to) ⇒ Object
- #lstat ⇒ Object
- #mkdir(mode = 0777) ⇒ Object
- #mkpath ⇒ Object
- #mtime ⇒ Object
- #names ⇒ Object
- #open(mode = 'r', perm = nil, &blk) ⇒ Object
- #opendir(&blk) ⇒ Object
- #owned? ⇒ Boolean
- #parent ⇒ Object
- #pipe? ⇒ Boolean
- #prefix ⇒ Object
- #read(len = nil, off = 0) ⇒ Object
- #readable? ⇒ Boolean
- #readable_real? ⇒ Boolean
- #readlines(sep = $/) ⇒ Object
- #readlink ⇒ Object
- #realpath ⇒ Object
- #relative? ⇒ Boolean
- #relative_path_from(base) ⇒ Object
- #rename(to) ⇒ Object
- #replace(path) ⇒ Object
- #rmdir ⇒ Object
- #rmtree ⇒ Object
- #root? ⇒ Boolean
- #setgid? ⇒ Boolean
- #setuid? ⇒ Boolean
- #size ⇒ Object
- #size? ⇒ Boolean
- #socket? ⇒ Boolean
- #stat ⇒ Object
- #sticky? ⇒ Boolean
- #symlink(to) ⇒ Object
- #symlink? ⇒ Boolean
- #sysopen(mode = 'r', perm = nil) ⇒ Object
- #to_a ⇒ Object (also: #segments)
- #to_path ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #touch ⇒ Object
- #truncate ⇒ Object
- #unlink ⇒ Object
- #utime(atime, mtime) ⇒ Object
- #world_readable? ⇒ Boolean
- #world_writable? ⇒ Boolean
- #writable? ⇒ Boolean
- #writable_real? ⇒ Boolean
- #zero? ⇒ Boolean
Constructor Details
#initialize(path) ⇒ Pathname
Returns a new instance of Pathname.
30 31 32 33 34 35 36 37 38 |
# File 'lib/fssm/pathname.rb', line 30 def initialize(path) if path =~ %r{\0} raise ArgumentError, "path cannot contain ASCII NULLs" end dememo super(path) end |
Class Method Details
.[](pattern) ⇒ Object
256 257 258 |
# File 'lib/fssm/pathname.rb', line 256 def [](pattern) Dir[pattern].map! {|path| new(path)} end |
.for(path) ⇒ Object
23 24 25 26 27 |
# File 'lib/fssm/pathname.rb', line 23 def for(path) path = path.is_a?(::FSSM::Pathname) ? path : new(path) path.dememo path end |
.glob(pattern, flags = 0) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/fssm/pathname.rb', line 244 def glob(pattern, flags=0) dirs = Dir.glob(pattern, flags) dirs.map! {|path| new(path)} if block_given? dirs.each {|dir| yield dir} nil else dirs end end |
.join(*parts) ⇒ Object
411 412 413 |
# File 'lib/fssm/pathname.rb', line 411 def join(*parts) new(File.join(*parts.reject {|p| p.empty? })) end |
.pwd ⇒ Object
260 261 262 |
# File 'lib/fssm/pathname.rb', line 260 def pwd new(Dir.pwd) end |
Instance Method Details
#+(path) ⇒ Object
97 98 99 |
# File 'lib/fssm/pathname.rb', line 97 def +(path) dup << path end |
#<<(path) ⇒ Object
101 102 103 |
# File 'lib/fssm/pathname.rb', line 101 def <<(path) replace(join(path).cleanpath!) end |
#absolute? ⇒ Boolean
93 94 95 |
# File 'lib/fssm/pathname.rb', line 93 def absolute? !relative? end |
#ascend ⇒ Object
65 66 67 68 69 70 |
# File 'lib/fssm/pathname.rb', line 65 def ascend parts = to_a parts.length.downto(1) do |i| yield self.class.join(parts[0, i]) end end |
#atime ⇒ Object
380 381 382 |
# File 'lib/fssm/pathname.rb', line 380 def atime File.atime(self) end |
#basename ⇒ Object
416 417 418 |
# File 'lib/fssm/pathname.rb', line 416 def basename self.class.new(File.basename(self)) end |
#blockdev? ⇒ Boolean
288 289 290 |
# File 'lib/fssm/pathname.rb', line 288 def blockdev? FileTest.blockdev?(self) end |
#chardev? ⇒ Boolean
292 293 294 |
# File 'lib/fssm/pathname.rb', line 292 def chardev? FileTest.chardev?(self) end |
#chdir ⇒ Object
281 282 283 284 |
# File 'lib/fssm/pathname.rb', line 281 def chdir blk = lambda { yield self } if block_given? Dir.chdir(self, &blk) end |
#chmod(mode) ⇒ Object
420 421 422 |
# File 'lib/fssm/pathname.rb', line 420 def chmod(mode) File.chmod(mode, self) end |
#chown(owner, group) ⇒ Object
424 425 426 |
# File 'lib/fssm/pathname.rb', line 424 def chown(owner, group) File.chown(owner, group, self) end |
#cleanpath ⇒ Object
130 131 132 |
# File 'lib/fssm/pathname.rb', line 130 def cleanpath dup.cleanpath! end |
#cleanpath! ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/fssm/pathname.rb', line 105 def cleanpath! parts = to_a final = [] parts.each do |part| case part when '.' then next when '..' then case final.last when '..' then final.push('..') when nil then final.push('..') else final.pop end else final.push(part) end end replace(final.empty? ? Dir.pwd : File.join(*final)) end |
#ctime ⇒ Object
384 385 386 |
# File 'lib/fssm/pathname.rb', line 384 def ctime File.ctime(self) end |
#dememo ⇒ Object
212 213 214 215 216 217 |
# File 'lib/fssm/pathname.rb', line 212 def dememo @set = nil @segments = nil @prefix = nil @names = nil end |
#descend ⇒ Object
72 73 74 75 76 77 |
# File 'lib/fssm/pathname.rb', line 72 def descend parts = to_a 1.upto(parts.length) do |i| yield self.class.join(parts[0, i]) end end |
#directory? ⇒ Boolean
296 297 298 |
# File 'lib/fssm/pathname.rb', line 296 def directory? FileTest.directory?(self) end |
#dirname ⇒ Object
428 429 430 |
# File 'lib/fssm/pathname.rb', line 428 def dirname self.class.new(File.dirname(self)) end |
#each_filename(&block) ⇒ Object
61 62 63 |
# File 'lib/fssm/pathname.rb', line 61 def each_filename(&block) to_a.each(&block) end |
#each_line(sep = $/, &blk) ⇒ Object
505 506 507 |
# File 'lib/fssm/pathname.rb', line 505 def each_line(sep = $/, &blk) IO.foreach(self, sep, &blk) end |
#entries ⇒ Object
265 266 267 |
# File 'lib/fssm/pathname.rb', line 265 def entries Dir.entries(self).map! {|e| FSSM::Pathname.new(e) } end |
#executable? ⇒ Boolean
300 301 302 |
# File 'lib/fssm/pathname.rb', line 300 def executable? FileTest.executable?(self) end |
#executable_real? ⇒ Boolean
304 305 306 |
# File 'lib/fssm/pathname.rb', line 304 def executable_real? FileTest.executable_real?(self) end |
#exists? ⇒ Boolean Also known as: exist?
308 309 310 |
# File 'lib/fssm/pathname.rb', line 308 def exists? FileTest.exists?(self) end |
#expand_path(from = nil) ⇒ Object
432 433 434 |
# File 'lib/fssm/pathname.rb', line 432 def (from = nil) self.class.new(File.(self, from)) end |
#extname ⇒ Object
436 437 438 |
# File 'lib/fssm/pathname.rb', line 436 def extname File.extname(self) end |
#file? ⇒ Boolean
312 313 314 |
# File 'lib/fssm/pathname.rb', line 312 def file? FileTest.file?(self) end |
#find ⇒ Object
523 524 525 |
# File 'lib/fssm/pathname.rb', line 523 def find Find.find(self) {|path| yield FSSM::Pathname.new(path) } end |
#fnmatch?(pat, flags = 0) ⇒ Boolean
440 441 442 |
# File 'lib/fssm/pathname.rb', line 440 def fnmatch?(pat, flags = 0) File.fnmatch(pat, self, flags) end |
#ftype ⇒ Object
388 389 390 |
# File 'lib/fssm/pathname.rb', line 388 def ftype File.ftype(self) end |
#grpowned? ⇒ Boolean
316 317 318 |
# File 'lib/fssm/pathname.rb', line 316 def grpowned? FileTest.grpowned?(self) end |
#join(*parts) ⇒ Object
444 445 446 |
# File 'lib/fssm/pathname.rb', line 444 def join(*parts) self.class.join(self, *parts) end |
#lchmod(mode) ⇒ Object
448 449 450 |
# File 'lib/fssm/pathname.rb', line 448 def lchmod(mode) File.lchmod(mode, self) end |
#lchown(owner, group) ⇒ Object
452 453 454 |
# File 'lib/fssm/pathname.rb', line 452 def lchown(owner, group) File.lchown(owner, group, self) end |
#link(to) ⇒ Object
456 457 458 |
# File 'lib/fssm/pathname.rb', line 456 def link(to) File.link(self, to) end |
#lstat ⇒ Object
392 393 394 |
# File 'lib/fssm/pathname.rb', line 392 def lstat File.lstat(self) end |
#mkdir(mode = 0777) ⇒ Object
269 270 271 |
# File 'lib/fssm/pathname.rb', line 269 def mkdir(mode = 0777) Dir.mkdir(self, mode) end |
#mkpath ⇒ Object
491 492 493 |
# File 'lib/fssm/pathname.rb', line 491 def mkpath self.class.new(FileUtils.mkpath(self)) end |
#mtime ⇒ Object
396 397 398 |
# File 'lib/fssm/pathname.rb', line 396 def mtime File.mtime(self) end |
#names ⇒ Object
207 208 209 210 |
# File 'lib/fssm/pathname.rb', line 207 def names set_prefix_and_names @names end |
#open(mode = 'r', perm = nil, &blk) ⇒ Object
460 461 462 |
# File 'lib/fssm/pathname.rb', line 460 def open(mode = 'r', perm = nil, &blk) File.open(self, mode, perm, &blk) end |
#opendir(&blk) ⇒ Object
273 274 275 |
# File 'lib/fssm/pathname.rb', line 273 def opendir(&blk) Dir.open(self, &blk) end |
#owned? ⇒ Boolean
320 321 322 |
# File 'lib/fssm/pathname.rb', line 320 def owned? FileTest.owned?(self) end |
#parent ⇒ Object
84 85 86 |
# File 'lib/fssm/pathname.rb', line 84 def parent self + '..' end |
#pipe? ⇒ Boolean
324 325 326 |
# File 'lib/fssm/pathname.rb', line 324 def pipe? FileTest.pipe?(self) end |
#prefix ⇒ Object
202 203 204 205 |
# File 'lib/fssm/pathname.rb', line 202 def prefix set_prefix_and_names @prefix end |
#read(len = nil, off = 0) ⇒ Object
509 510 511 |
# File 'lib/fssm/pathname.rb', line 509 def read(len = nil, off = 0) IO.read(self, len, off) end |
#readable? ⇒ Boolean
328 329 330 |
# File 'lib/fssm/pathname.rb', line 328 def readable? FileTest.readable?(self) end |
#readable_real? ⇒ Boolean
332 333 334 |
# File 'lib/fssm/pathname.rb', line 332 def readable_real? FileTest.readable_real?(self) end |
#readlines(sep = $/) ⇒ Object
513 514 515 |
# File 'lib/fssm/pathname.rb', line 513 def readlines(sep = $/) IO.readlines(self, sep) end |
#readlink ⇒ Object
464 465 466 |
# File 'lib/fssm/pathname.rb', line 464 def readlink self.class.new(File.readlink(self)) end |
#realpath ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/fssm/pathname.rb', line 134 def realpath raise unless self.exist? if File.symlink?(self) file = self.dup while true file = File.join(File.dirname(file), File.readlink(file)) break unless File.symlink?(file) end self.class.new(file).clean else self.class.new(Dir.pwd) + self end end |
#relative? ⇒ Boolean
88 89 90 91 |
# File 'lib/fssm/pathname.rb', line 88 def relative? set_prefix_and_names @prefix.empty? end |
#relative_path_from(base) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fssm/pathname.rb', line 151 def relative_path_from(base) base = self.class.for(base) if self.absolute? != base.absolute? raise ArgumentError, 'no relative path between a relative and absolute' end if self.prefix != base.prefix raise ArgumentError, "different prefix: #{@prefix.inspect} and #{base.prefix.inspect}" end base = base.cleanpath!.segments dest = dup.cleanpath!.segments while !dest.empty? && !base.empty? && dest[0] == base[0] base.shift dest.shift end base.shift if base[0] == '.' dest.shift if dest[0] == '.' if base.include?('..') raise ArgumentError, "base directory may not contain '..'" end path = base.fill('..') + dest path = self.class.join(*path) path = self.class.new('.') if path.empty? path end |
#rename(to) ⇒ Object
468 469 470 471 |
# File 'lib/fssm/pathname.rb', line 468 def rename(to) File.rename(self, to) replace(to) end |
#replace(path) ⇒ Object
184 185 186 187 188 189 190 191 192 |
# File 'lib/fssm/pathname.rb', line 184 def replace(path) if path =~ %r{\0} raise ArgumentError, "path cannot contain ASCII NULLs" end dememo super(path) end |
#rmdir ⇒ Object
277 278 279 |
# File 'lib/fssm/pathname.rb', line 277 def rmdir Dir.rmdir(self) end |
#rmtree ⇒ Object
495 496 497 |
# File 'lib/fssm/pathname.rb', line 495 def rmtree self.class.new(FileUtils.rmtree(self).first) end |
#root? ⇒ Boolean
79 80 81 82 |
# File 'lib/fssm/pathname.rb', line 79 def root? set_prefix_and_names @names.empty? && !@prefix.empty? end |
#setgid? ⇒ Boolean
336 337 338 |
# File 'lib/fssm/pathname.rb', line 336 def setgid? FileTest.setgit?(self) end |
#setuid? ⇒ Boolean
340 341 342 |
# File 'lib/fssm/pathname.rb', line 340 def setuid? FileTest.setuid?(self) end |
#size ⇒ Object
473 474 475 |
# File 'lib/fssm/pathname.rb', line 473 def size File.size(self) end |
#size? ⇒ Boolean
477 478 479 |
# File 'lib/fssm/pathname.rb', line 477 def size? File.size?(self) end |
#socket? ⇒ Boolean
344 345 346 |
# File 'lib/fssm/pathname.rb', line 344 def socket? FileTest.socket?(self) end |
#stat ⇒ Object
400 401 402 |
# File 'lib/fssm/pathname.rb', line 400 def stat File.stat(self) end |
#sticky? ⇒ Boolean
348 349 350 |
# File 'lib/fssm/pathname.rb', line 348 def sticky? FileTest.sticky?(self) end |
#symlink(to) ⇒ Object
481 482 483 |
# File 'lib/fssm/pathname.rb', line 481 def symlink(to) File.symlink(self, to) end |
#symlink? ⇒ Boolean
352 353 354 |
# File 'lib/fssm/pathname.rb', line 352 def symlink? FileTest.symlink?(self) end |
#sysopen(mode = 'r', perm = nil) ⇒ Object
517 518 519 |
# File 'lib/fssm/pathname.rb', line 517 def sysopen(mode = 'r', perm = nil) IO.sysopen(self, mode, perm) end |
#to_a ⇒ Object Also known as: segments
50 51 52 53 54 55 56 57 |
# File 'lib/fssm/pathname.rb', line 50 def to_a return @segments if @segments set_prefix_and_names @segments = @names.dup @segments.delete('.') @segments.unshift(@prefix) unless @prefix.empty? @segments end |
#to_path ⇒ Object
40 41 42 |
# File 'lib/fssm/pathname.rb', line 40 def to_path self end |
#to_s ⇒ Object Also known as: to_str
44 45 46 |
# File 'lib/fssm/pathname.rb', line 44 def to_s "#{self}" end |
#touch ⇒ Object
499 500 501 |
# File 'lib/fssm/pathname.rb', line 499 def touch self.class.new(FileUtils.touch(self).first) end |
#truncate ⇒ Object
485 486 487 |
# File 'lib/fssm/pathname.rb', line 485 def truncate File.truncate(self) end |
#unlink ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/fssm/pathname.rb', line 194 def unlink Dir.unlink(self) true rescue Errno::ENOTDIR File.unlink(self) true end |
#utime(atime, mtime) ⇒ Object
404 405 406 |
# File 'lib/fssm/pathname.rb', line 404 def utime(atime, mtime) File.utime(self, atime, mtime) end |
#world_readable? ⇒ Boolean
356 357 358 |
# File 'lib/fssm/pathname.rb', line 356 def world_readable? FileTest.world_readable?(self) end |
#world_writable? ⇒ Boolean
360 361 362 |
# File 'lib/fssm/pathname.rb', line 360 def world_writable? FileTest.world_writable?(self) end |
#writable? ⇒ Boolean
364 365 366 |
# File 'lib/fssm/pathname.rb', line 364 def writable? FileTest.writable?(self) end |
#writable_real? ⇒ Boolean
368 369 370 |
# File 'lib/fssm/pathname.rb', line 368 def writable_real? FileTest.writable_real?(self) end |
#zero? ⇒ Boolean
372 373 374 |
# File 'lib/fssm/pathname.rb', line 372 def zero? FileTest.zero?(self) end |