class Pathname
we enhance pathname to make our code more readable
Public Instance Methods
Source
# File lib/extend/pathname.rb, line 93 def as_backup Pathname.new File.join(dirname, "#{basename}.lace.bak") end
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
Source
# File lib/extend/pathname.rb, line 59 def chmod_R perms require 'fileutils' FileUtils.chmod_R perms, to_s end
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
Source
# File lib/extend/pathname.rb, line 76 def resolved_path self.symlink? ? dirname+readlink : self end
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
Source
# 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
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
Source
# File lib/extend/pathname.rb, line 38 def stem File.basename((path = to_s), extname(path)) end
for filetypes we support, basename without extension
Source
# File lib/extend/pathname.rb, line 72 def subdirs children.select{ |child| child.directory? } end