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
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 |
.pwd ⇒ Object
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
91 92 93 |
# File 'lib/fssm/pathname.rb', line 91 def absolute? !relative? end |
#ascend ⇒ Object
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 |
#atime ⇒ Object
297 |
# File 'lib/fssm/pathname.rb', line 297 def atime; File.atime(self); end |
#basename ⇒ Object
313 |
# File 'lib/fssm/pathname.rb', line 313 def basename; self.class.new(File.basename(self)); end |
#blockdev? ⇒ Boolean
270 |
# File 'lib/fssm/pathname.rb', line 270 def blockdev?; FileTest.blockdev?(self); end |
#chardev? ⇒ Boolean
271 |
# File 'lib/fssm/pathname.rb', line 271 def chardev?; FileTest.chardev?(self); end |
#chdir ⇒ Object
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 |
#cleanpath ⇒ Object
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 |
#ctime ⇒ Object
298 |
# File 'lib/fssm/pathname.rb', line 298 def ctime; File.ctime(self); end |
#dememo ⇒ Object
205 206 207 208 209 210 |
# File 'lib/fssm/pathname.rb', line 205 def dememo @set = nil @segments = nil @prefix = nil @names = nil end |
#descend ⇒ Object
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
272 |
# File 'lib/fssm/pathname.rb', line 272 def directory?; FileTest.directory?(self); end |
#dirname ⇒ Object
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 |
#entries ⇒ Object
258 |
# File 'lib/fssm/pathname.rb', line 258 def entries; Dir.entries(self).map! {|e| FSSM::Pathname.new(e) }; end |
#executable? ⇒ Boolean
273 |
# File 'lib/fssm/pathname.rb', line 273 def executable?; FileTest.executable?(self); end |
#executable_real? ⇒ Boolean
274 |
# File 'lib/fssm/pathname.rb', line 274 def executable_real?; FileTest.executable_real?(self); end |
#exists? ⇒ Boolean Also known as: exist?
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 (from = nil); self.class.new(File.(self, from)); end |
#extname ⇒ Object
318 |
# File 'lib/fssm/pathname.rb', line 318 def extname; File.extname(self); end |
#file? ⇒ Boolean
276 |
# File 'lib/fssm/pathname.rb', line 276 def file?; FileTest.file?(self); end |
#find ⇒ Object
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
319 |
# File 'lib/fssm/pathname.rb', line 319 def fnmatch?(pat, flags = 0); File.fnmatch(pat, self, flags); end |
#ftype ⇒ Object
299 |
# File 'lib/fssm/pathname.rb', line 299 def ftype; File.ftype(self); end |
#grpowned? ⇒ 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 |
#link(to) ⇒ Object
323 |
# File 'lib/fssm/pathname.rb', line 323 def link(to); File.link(self, to); end |
#lstat ⇒ Object
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 |
#mkpath ⇒ Object
334 |
# File 'lib/fssm/pathname.rb', line 334 def mkpath; self.class.new(FileUtils.mkpath(self)); end |
#mtime ⇒ Object
301 |
# File 'lib/fssm/pathname.rb', line 301 def mtime; File.mtime(self); end |
#names ⇒ Object
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
278 |
# File 'lib/fssm/pathname.rb', line 278 def owned?; FileTest.owned?(self); end |
#parent ⇒ Object
82 83 84 |
# File 'lib/fssm/pathname.rb', line 82 def parent self + '..' end |
#pipe? ⇒ Boolean
279 |
# File 'lib/fssm/pathname.rb', line 279 def pipe?; FileTest.pipe?(self); end |
#prefix ⇒ Object
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
280 |
# File 'lib/fssm/pathname.rb', line 280 def readable?; FileTest.readable?(self); end |
#readable_real? ⇒ 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 |
#readlink ⇒ Object
325 |
# File 'lib/fssm/pathname.rb', line 325 def readlink; self.class.new(File.readlink(self)); end |
#realpath ⇒ Object
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
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 |
#rmdir ⇒ Object
261 |
# File 'lib/fssm/pathname.rb', line 261 def rmdir; Dir.rmdir(self); end |
#rmtree ⇒ Object
335 |
# File 'lib/fssm/pathname.rb', line 335 def rmtree; self.class.new(FileUtils.rmtree(self).first); end |
#root? ⇒ 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
282 |
# File 'lib/fssm/pathname.rb', line 282 def setgid?; FileTest.setgit?(self); end |
#setuid? ⇒ Boolean
283 |
# File 'lib/fssm/pathname.rb', line 283 def setuid?; FileTest.setuid?(self); end |
#size ⇒ Object
327 |
# File 'lib/fssm/pathname.rb', line 327 def size; File.size(self); end |
#size? ⇒ Boolean
328 |
# File 'lib/fssm/pathname.rb', line 328 def size?; File.size?(self); end |
#socket? ⇒ Boolean
284 |
# File 'lib/fssm/pathname.rb', line 284 def socket?; FileTest.socket?(self); end |
#stat ⇒ Object
302 |
# File 'lib/fssm/pathname.rb', line 302 def stat; File.stat(self); end |
#sticky? ⇒ Boolean
285 |
# File 'lib/fssm/pathname.rb', line 285 def sticky?; FileTest.sticky?(self); end |
#symlink(to) ⇒ Object
329 |
# File 'lib/fssm/pathname.rb', line 329 def symlink(to); File.symlink(self, to); end |
#symlink? ⇒ 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_a ⇒ Object 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_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
336 |
# File 'lib/fssm/pathname.rb', line 336 def touch; self.class.new(FileUtils.touch(self).first); end |
#truncate ⇒ Object
330 |
# File 'lib/fssm/pathname.rb', line 330 def truncate; File.truncate(self); end |
#unlink ⇒ Object
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
287 |
# File 'lib/fssm/pathname.rb', line 287 def world_readable?; FileTest.world_readable?(self); end |
#world_writable? ⇒ Boolean
288 |
# File 'lib/fssm/pathname.rb', line 288 def world_writable?; FileTest.world_writable?(self); end |
#writable? ⇒ Boolean
289 |
# File 'lib/fssm/pathname.rb', line 289 def writable?; FileTest.writable?(self); end |
#writable_real? ⇒ Boolean
290 |
# File 'lib/fssm/pathname.rb', line 290 def writable_real?; FileTest.writable_real?(self); end |
#zero? ⇒ Boolean
291 |
# File 'lib/fssm/pathname.rb', line 291 def zero?; FileTest.zero?(self); end |