Class: FSSM::Pathname

Inherits:
String
  • Object
show all
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

Instance Method Summary collapse

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

.pwdObject



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

Returns:

  • (Boolean)


93
94
95
# File 'lib/fssm/pathname.rb', line 93

def absolute?
  !relative?
end

#ascendObject



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

#atimeObject



380
381
382
# File 'lib/fssm/pathname.rb', line 380

def atime
  File.atime(self)
end

#basenameObject



416
417
418
# File 'lib/fssm/pathname.rb', line 416

def basename
  self.class.new(File.basename(self))
end

#blockdev?Boolean

Returns:

  • (Boolean)


288
289
290
# File 'lib/fssm/pathname.rb', line 288

def blockdev?
  FileTest.blockdev?(self)
end

#chardev?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/fssm/pathname.rb', line 292

def chardev?
  FileTest.chardev?(self)
end

#chdirObject



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

#cleanpathObject



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

#ctimeObject



384
385
386
# File 'lib/fssm/pathname.rb', line 384

def ctime
  File.ctime(self)
end

#dememoObject



212
213
214
215
216
217
# File 'lib/fssm/pathname.rb', line 212

def dememo
  @set = nil
  @segments = nil
  @prefix = nil
  @names = nil
end

#descendObject



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

Returns:

  • (Boolean)


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

def directory?
  FileTest.directory?(self)
end

#dirnameObject



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

#entriesObject



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

def entries
  Dir.entries(self).map! {|e| FSSM::Pathname.new(e) }
end

#executable?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/fssm/pathname.rb', line 300

def executable?
  FileTest.executable?(self)
end

#executable_real?Boolean

Returns:

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

Returns:

  • (Boolean)


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 expand_path(from = nil)
  self.class.new(File.expand_path(self, from))
end

#extnameObject



436
437
438
# File 'lib/fssm/pathname.rb', line 436

def extname
  File.extname(self)
end

#file?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/fssm/pathname.rb', line 312

def file?
  FileTest.file?(self)
end

#findObject



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

Returns:

  • (Boolean)


440
441
442
# File 'lib/fssm/pathname.rb', line 440

def fnmatch?(pat, flags = 0)
  File.fnmatch(pat, self, flags)
end

#ftypeObject



388
389
390
# File 'lib/fssm/pathname.rb', line 388

def ftype
  File.ftype(self)
end

#grpowned?Boolean

Returns:

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


456
457
458
# File 'lib/fssm/pathname.rb', line 456

def link(to)
  File.link(self, to)
end

#lstatObject



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

#mkpathObject



491
492
493
# File 'lib/fssm/pathname.rb', line 491

def mkpath
  self.class.new(FileUtils.mkpath(self))
end

#mtimeObject



396
397
398
# File 'lib/fssm/pathname.rb', line 396

def mtime
  File.mtime(self)
end

#namesObject



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

Returns:

  • (Boolean)


320
321
322
# File 'lib/fssm/pathname.rb', line 320

def owned?
  FileTest.owned?(self)
end

#parentObject



84
85
86
# File 'lib/fssm/pathname.rb', line 84

def parent
  self + '..'
end

#pipe?Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/fssm/pathname.rb', line 324

def pipe?
  FileTest.pipe?(self)
end

#prefixObject



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

Returns:

  • (Boolean)


328
329
330
# File 'lib/fssm/pathname.rb', line 328

def readable?
  FileTest.readable?(self)
end

#readable_real?Boolean

Returns:

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


464
465
466
# File 'lib/fssm/pathname.rb', line 464

def readlink
  self.class.new(File.readlink(self))
end

#realpathObject



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

Returns:

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

#rmdirObject



277
278
279
# File 'lib/fssm/pathname.rb', line 277

def rmdir
  Dir.rmdir(self)
end

#rmtreeObject



495
496
497
# File 'lib/fssm/pathname.rb', line 495

def rmtree
  self.class.new(FileUtils.rmtree(self).first)
end

#root?Boolean

Returns:

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

Returns:

  • (Boolean)


336
337
338
# File 'lib/fssm/pathname.rb', line 336

def setgid?
  FileTest.setgit?(self)
end

#setuid?Boolean

Returns:

  • (Boolean)


340
341
342
# File 'lib/fssm/pathname.rb', line 340

def setuid?
  FileTest.setuid?(self)
end

#sizeObject



473
474
475
# File 'lib/fssm/pathname.rb', line 473

def size
  File.size(self)
end

#size?Boolean

Returns:

  • (Boolean)


477
478
479
# File 'lib/fssm/pathname.rb', line 477

def size?
  File.size?(self)
end

#socket?Boolean

Returns:

  • (Boolean)


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

def socket?
  FileTest.socket?(self)
end

#statObject



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

def stat
  File.stat(self)
end

#sticky?Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/fssm/pathname.rb', line 348

def sticky?
  FileTest.sticky?(self)
end


481
482
483
# File 'lib/fssm/pathname.rb', line 481

def symlink(to)
  File.symlink(self, to)
end

#symlink?Boolean

Returns:

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



40
41
42
# File 'lib/fssm/pathname.rb', line 40

def to_path
  self
end

#to_sObject Also known as: to_str



44
45
46
# File 'lib/fssm/pathname.rb', line 44

def to_s
  "#{self}"
end

#touchObject



499
500
501
# File 'lib/fssm/pathname.rb', line 499

def touch
  self.class.new(FileUtils.touch(self).first)
end

#truncateObject



485
486
487
# File 'lib/fssm/pathname.rb', line 485

def truncate
  File.truncate(self)
end


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

Returns:

  • (Boolean)


356
357
358
# File 'lib/fssm/pathname.rb', line 356

def world_readable?
  FileTest.world_readable?(self)
end

#world_writable?Boolean

Returns:

  • (Boolean)


360
361
362
# File 'lib/fssm/pathname.rb', line 360

def world_writable?
  FileTest.world_writable?(self)
end

#writable?Boolean

Returns:

  • (Boolean)


364
365
366
# File 'lib/fssm/pathname.rb', line 364

def writable?
  FileTest.writable?(self)
end

#writable_real?Boolean

Returns:

  • (Boolean)


368
369
370
# File 'lib/fssm/pathname.rb', line 368

def writable_real?
  FileTest.writable_real?(self)
end

#zero?Boolean

Returns:

  • (Boolean)


372
373
374
# File 'lib/fssm/pathname.rb', line 372

def zero?
  FileTest.zero?(self)
end