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



249
250
251
# File 'lib/fssm/pathname.rb', line 249

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



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/fssm/pathname.rb', line 237

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



308
309
310
# File 'lib/fssm/pathname.rb', line 308

def join(*parts)
  new(File.join(*parts.reject {|p| p.empty? }))
end

.pwdObject



253
254
255
# File 'lib/fssm/pathname.rb', line 253

def pwd
  new(Dir.pwd)
end

Instance Method Details

#+(path) ⇒ Object



95
96
97
# File 'lib/fssm/pathname.rb', line 95

def +(path)
  dup << path
end

#<<(path) ⇒ Object



99
100
101
# File 'lib/fssm/pathname.rb', line 99

def <<(path)
  replace(join(path).cleanpath!)
end

#absolute?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/fssm/pathname.rb', line 91

def absolute?
  !relative?
end

#ascendObject



63
64
65
66
67
68
# File 'lib/fssm/pathname.rb', line 63

def ascend
  parts = to_a
  parts.length.downto(1) do |i|
    yield self.class.join(parts[0, i])
  end
end

#atimeObject



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

def atime; File.atime(self); end

#basenameObject



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

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

#blockdev?Boolean

Returns:

  • (Boolean)


270
# File 'lib/fssm/pathname.rb', line 270

def blockdev?; FileTest.blockdev?(self); end

#chardev?Boolean

Returns:

  • (Boolean)


271
# File 'lib/fssm/pathname.rb', line 271

def chardev?; FileTest.chardev?(self); end

#chdirObject



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

def chdir 
  blk = lambda { yield self } if block_given?
  Dir.chdir(self, &blk)
end

#chmod(mode) ⇒ Object



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

def chmod(mode); File.chmod(mode, self); end

#chown(owner, group) ⇒ Object



315
# File 'lib/fssm/pathname.rb', line 315

def chown(owner, group); File.chown(owner, group, self); end

#cleanpathObject



123
124
125
# File 'lib/fssm/pathname.rb', line 123

def cleanpath
  dup.cleanpath!
end

#cleanpath!Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fssm/pathname.rb', line 103

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



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

def ctime; File.ctime(self); end

#dememoObject



205
206
207
208
209
210
# File 'lib/fssm/pathname.rb', line 205

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

#descendObject



70
71
72
73
74
75
# File 'lib/fssm/pathname.rb', line 70

def descend
  parts = to_a
  1.upto(parts.length) do |i|
    yield self.class.join(parts[0, i])
  end
end

#directory?Boolean

Returns:

  • (Boolean)


272
# File 'lib/fssm/pathname.rb', line 272

def directory?; FileTest.directory?(self); end

#dirnameObject



316
# File 'lib/fssm/pathname.rb', line 316

def dirname; self.class.new(File.dirname(self)); end

#each_filename(&block) ⇒ Object



59
60
61
# File 'lib/fssm/pathname.rb', line 59

def each_filename(&block)
  to_a.each(&block)
end

#each_line(sep = $/, &blk) ⇒ Object



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

def each_line(sep = $/, &blk); IO.foreach(self, sep, &blk); end

#entriesObject



258
# File 'lib/fssm/pathname.rb', line 258

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

#executable?Boolean

Returns:

  • (Boolean)


273
# File 'lib/fssm/pathname.rb', line 273

def executable?; FileTest.executable?(self); end

#executable_real?Boolean

Returns:

  • (Boolean)


274
# File 'lib/fssm/pathname.rb', line 274

def executable_real?; FileTest.executable_real?(self); end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)


275
# File 'lib/fssm/pathname.rb', line 275

def exists?; FileTest.exists?(self); end

#expand_path(from = nil) ⇒ Object



317
# File 'lib/fssm/pathname.rb', line 317

def expand_path(from = nil); self.class.new(File.expand_path(self, from)); end

#extnameObject



318
# File 'lib/fssm/pathname.rb', line 318

def extname; File.extname(self); end

#file?Boolean

Returns:

  • (Boolean)


276
# File 'lib/fssm/pathname.rb', line 276

def file?; FileTest.file?(self); end

#findObject



347
# File 'lib/fssm/pathname.rb', line 347

def find; Find.find(self) {|path| yield FSSM::Pathname.new(path) }; end

#fnmatch?(pat, flags = 0) ⇒ Boolean

Returns:

  • (Boolean)


319
# File 'lib/fssm/pathname.rb', line 319

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

#ftypeObject



299
# File 'lib/fssm/pathname.rb', line 299

def ftype; File.ftype(self); end

#grpowned?Boolean

Returns:

  • (Boolean)


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

def grpowned?; FileTest.grpowned?(self); end

#join(*parts) ⇒ Object



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

def join(*parts); self.class.join(self, *parts); end

#lchmod(mode) ⇒ Object



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

def lchmod(mode); File.lchmod(mode, self); end

#lchown(owner, group) ⇒ Object



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

def lchown(owner, group); File.lchown(owner, group, self); end


323
# File 'lib/fssm/pathname.rb', line 323

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

#lstatObject



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

def lstat; File.lstat(self); end

#mkdir(mode = 0777) ⇒ Object



259
# File 'lib/fssm/pathname.rb', line 259

def mkdir(mode = 0777); Dir.mkdir(self, mode); end

#mkpathObject



334
# File 'lib/fssm/pathname.rb', line 334

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

#mtimeObject



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

def mtime; File.mtime(self); end

#namesObject



200
201
202
203
# File 'lib/fssm/pathname.rb', line 200

def names
  set_prefix_and_names
  @names
end

#open(mode = 'r', perm = nil, &blk) ⇒ Object



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

def open(mode = 'r', perm = nil, &blk); File.open(self, mode, perm, &blk); end

#opendir(&blk) ⇒ Object



260
# File 'lib/fssm/pathname.rb', line 260

def opendir(&blk); Dir.open(self, &blk); end

#owned?Boolean

Returns:

  • (Boolean)


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

def owned?; FileTest.owned?(self); end

#parentObject



82
83
84
# File 'lib/fssm/pathname.rb', line 82

def parent
  self + '..'
end

#pipe?Boolean

Returns:

  • (Boolean)


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

def pipe?; FileTest.pipe?(self); end

#prefixObject



195
196
197
198
# File 'lib/fssm/pathname.rb', line 195

def prefix
  set_prefix_and_names
  @prefix
end

#read(len = nil, off = 0) ⇒ Object



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

def read(len = nil, off = 0); IO.read(self, len, off); end

#readable?Boolean

Returns:

  • (Boolean)


280
# File 'lib/fssm/pathname.rb', line 280

def readable?; FileTest.readable?(self); end

#readable_real?Boolean

Returns:

  • (Boolean)


281
# File 'lib/fssm/pathname.rb', line 281

def readable_real?; FileTest.readable_real?(self); end

#readlines(sep = $/) ⇒ Object



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

def readlines(sep = $/); IO.readlines(self, sep); end


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

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

#realpathObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/fssm/pathname.rb', line 127

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)


86
87
88
89
# File 'lib/fssm/pathname.rb', line 86

def relative?
  set_prefix_and_names
  @prefix.empty?
end

#relative_path_from(base) ⇒ Object



144
145
146
147
148
149
150
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
# File 'lib/fssm/pathname.rb', line 144

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



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

def rename(to); File.rename(self, to); replace(to); end

#replace(path) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/fssm/pathname.rb', line 177

def replace(path)      
  if path =~ %r{\0}
    raise ArgumentError, "path cannot contain ASCII NULLs"
  end
  
  dememo
  
  super(path)
end

#rmdirObject



261
# File 'lib/fssm/pathname.rb', line 261

def rmdir; Dir.rmdir(self); end

#rmtreeObject



335
# File 'lib/fssm/pathname.rb', line 335

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

#root?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/fssm/pathname.rb', line 77

def root?
  set_prefix_and_names
  @names.empty? && !@prefix.empty?
end

#setgid?Boolean

Returns:

  • (Boolean)


282
# File 'lib/fssm/pathname.rb', line 282

def setgid?; FileTest.setgit?(self); end

#setuid?Boolean

Returns:

  • (Boolean)


283
# File 'lib/fssm/pathname.rb', line 283

def setuid?; FileTest.setuid?(self); end

#sizeObject



327
# File 'lib/fssm/pathname.rb', line 327

def size; File.size(self); end

#size?Boolean

Returns:

  • (Boolean)


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

def size?; File.size?(self); end

#socket?Boolean

Returns:

  • (Boolean)


284
# File 'lib/fssm/pathname.rb', line 284

def socket?; FileTest.socket?(self); end

#statObject



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

def stat; File.stat(self); end

#sticky?Boolean

Returns:

  • (Boolean)


285
# File 'lib/fssm/pathname.rb', line 285

def sticky?; FileTest.sticky?(self); end


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

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

#symlink?Boolean

Returns:

  • (Boolean)


286
# File 'lib/fssm/pathname.rb', line 286

def symlink?; FileTest.symlink?(self); end

#sysopen(mode = 'r', perm = nil) ⇒ Object



343
# File 'lib/fssm/pathname.rb', line 343

def sysopen(mode = 'r', perm = nil); IO.sysopen(self, mode, perm); end

#to_aObject Also known as: segments



49
50
51
52
53
54
55
56
# File 'lib/fssm/pathname.rb', line 49

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



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

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

#truncateObject



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

def truncate; File.truncate(self); end


187
188
189
190
191
192
193
# File 'lib/fssm/pathname.rb', line 187

def unlink
  Dir.unlink(self)
  true
rescue Errno::ENOTDIR
  File.unlink(self)
  true
end

#utime(atime, mtime) ⇒ Object



303
# File 'lib/fssm/pathname.rb', line 303

def utime(atime, mtime); File.utime(self, atime, mtime); end

#world_readable?Boolean

Returns:

  • (Boolean)


287
# File 'lib/fssm/pathname.rb', line 287

def world_readable?; FileTest.world_readable?(self); end

#world_writable?Boolean

Returns:

  • (Boolean)


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

def world_writable?; FileTest.world_writable?(self); end

#writable?Boolean

Returns:

  • (Boolean)


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

def writable?; FileTest.writable?(self); end

#writable_real?Boolean

Returns:

  • (Boolean)


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

def writable_real?; FileTest.writable_real?(self); end

#zero?Boolean

Returns:

  • (Boolean)


291
# File 'lib/fssm/pathname.rb', line 291

def zero?; FileTest.zero?(self); end