Class: Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/drupid/extend/pathname.rb

Overview

Code borrowed from Homebrew ;)

Instance Method Summary collapse

Instance Method Details

#cdObject



79
80
81
# File 'lib/drupid/extend/pathname.rb', line 79

def cd
  Dir.chdir(self) { yield }
end

#compression_typeObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/drupid/extend/pathname.rb', line 50

def compression_type
  return nil if self.directory?
  # Don't treat jars or wars as compressed
  return nil if self.extname == '.jar'
  return nil if self.extname == '.war'

  # OS X installer package
  return :pkg if self.extname == '.pkg'

  # Get enough of the file to detect common file types
  # POSIX tar magic has a 257 byte offset
  magic_bytes = nil
  File.open(self) { |f| magic_bytes = f.read(262) }

  # magic numbers stolen from /usr/share/file/magic/
  case magic_bytes
  when /^PK\003\004/   then :zip
  when /^\037\213/     then :gzip
  when /^BZh/          then :bzip2
  when /^\037\235/     then :compress
  when /^.{257}ustar/  then :tar
  when /^\xFD7zXZ\x00/ then :xz
  when /^Rar!/         then :rar
  else
    # Assume it is not an archive
    nil
  end
end

#ditto(dst) ⇒ Object

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/drupid/extend/pathname.rb', line 85

def ditto dst
  d = Pathname.new(dst)
  if file?
    FileUtils.cp to_s, d.to_s, :verbose => $DEBUG
    return (d.directory?) ? d+basename : d
  else
    d.mkpath
    FileUtils.cp_r to_s + '/.', d.to_s, :verbose => $DEBUG
    return d
  end
end

#extnameObject



99
100
101
102
103
# File 'lib/drupid/extend/pathname.rb', line 99

def extname
  /(\.(tar|cpio)\.(gz|bz2|xz|Z))$/.match to_s
  return $1 if $1
  return File.extname(to_s)
end

#extname_oldObject

extended to support common double extensions



98
# File 'lib/drupid/extend/pathname.rb', line 98

alias extname_old extname

#sub_ext(repl) ⇒ Object

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

If self has no extension part, repl is appended.



111
112
113
114
# File 'lib/drupid/extend/pathname.rb', line 111

def sub_ext(repl)
  ext = File.extname(@path)
  self.class.new(@path.chomp(ext) + repl)
end