class Pathname

Code borrowed from Homebrew ;)

Public Instance Methods

cd() { || ... } click to toggle source
   # File lib/drupid/extend/pathname.rb
78 def cd
79   Dir.chdir(self) { yield }
80 end
compression_type() click to toggle source
   # File lib/drupid/extend/pathname.rb
49 def compression_type
50   return nil if self.directory?
51   # Don't treat jars or wars as compressed
52   return nil if self.extname == '.jar'
53   return nil if self.extname == '.war'
54 
55   # OS X installer package
56   return :pkg if self.extname == '.pkg'
57 
58   # Get enough of the file to detect common file types
59   # POSIX tar magic has a 257 byte offset
60   magic_bytes = nil
61   File.open(self) { |f| magic_bytes = f.read(262) }
62 
63   # magic numbers stolen from /usr/share/file/magic/
64   case magic_bytes
65   when /^PK\003\004/   then :zip
66   when /^\037\213/     then :gzip
67   when /^BZh/          then :bzip2
68   when /^\037\235/     then :compress
69   when /^.{257}ustar/  then :tar
70   when /^\xFD7zXZ\x00/ then :xz
71   when /^Rar!/         then :rar
72   else
73     # Assume it is not an archive
74     nil
75   end
76 end
ditto(dst) click to toggle source

Copies a file to another location or the content of a directory into the specified directory.

   # File lib/drupid/extend/pathname.rb
84 def ditto dst
85   d = Pathname.new(dst)
86   if file?
87     FileUtils.cp to_s, d.to_s, :verbose => $DEBUG
88     return (d.directory?) ? d+basename : d
89   else
90     d.mkpath
91     FileUtils.cp_r to_s + '/.', d.to_s, :verbose => $DEBUG
92     return d
93   end
94 end
extname() click to toggle source
    # File lib/drupid/extend/pathname.rb
 98 def extname
 99   /(\.(tar|cpio)\.(gz|bz2|xz|Z))$/.match to_s
100   return $1 if $1
101   return File.extname(to_s)
102 end
Also aliased as: extname_old
extname_old()

extended to support common double extensions

Alias for: extname
sub_ext(repl) click to toggle source

Return a pathname which the extension of the basename is substituted by repl.

If self has no extension part, repl is appended.

    # File lib/drupid/extend/pathname.rb
110 def sub_ext(repl)
111   ext = File.extname(@path)
112   self.class.new(@path.chomp(ext) + repl)
113 end