class Pathname

we enhance pathname to make our code more readable

Public Instance Methods

/(that) click to toggle source
# File lib/extend/pathname.rb, line 89
def / that
  join that.to_s
end
as_backup() click to toggle source
# File lib/extend/pathname.rb, line 93
def as_backup
  Pathname.new File.join(dirname, "#{basename}.lace.bak")
end
as_dotfile(base_folder, subtract) click to toggle source
# File lib/extend/pathname.rb, line 97
def as_dotfile base_folder, subtract
  Pathname.new File.join(base_folder, ".#{to_path.gsub(subtract, "")[1..-1]}")
end
cd() { || ... } click to toggle source
# File lib/extend/pathname.rb, line 68
def cd
  Dir.chdir(self){ yield }
end
chmod_R(perms) click to toggle source
# File lib/extend/pathname.rb, line 59
def chmod_R perms
  require 'fileutils'
  FileUtils.chmod_R perms, to_s
end
cp(dst) click to toggle source
# File lib/extend/pathname.rb, line 28
def cp dst
  if file?
    FileUtils.cp to_s, dst
  else
    FileUtils.cp_r to_s, dst
  end
  return dst
end
resolved_path() click to toggle source
# File lib/extend/pathname.rb, line 76
def resolved_path
  self.symlink? ? dirname+readlink : self
end
resolved_path_exists?() click to toggle source
# File lib/extend/pathname.rb, line 80
def resolved_path_exists?
  link = readlink
rescue ArgumentError
  # The link target contains NUL bytes
  false
else
  (dirname+link).exist?
end
rmdir_if_possible() click to toggle source

I don't trust the children.length == 0 check particularly, not to mention it is slow to enumerate the whole directory just to see if it is empty, instead rely on good ol' libc and the filesystem

# File lib/extend/pathname.rb, line 45
def rmdir_if_possible
  rmdir
  true
rescue Errno::ENOTEMPTY
  if (ds_store = self+'.DS_Store').exist? && children.length == 1
    ds_store.unlink
    retry
  else
    false
  end
rescue Errno::EACCES, Errno::ENOENT
  false
end
stem() click to toggle source

for filetypes we support, basename without extension

# File lib/extend/pathname.rb, line 38
def stem
  File.basename((path = to_s), extname(path))
end
subdirs() click to toggle source
# File lib/extend/pathname.rb, line 72
def subdirs
  children.select{ |child| child.directory? }
end