Class: Pathname

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

Overview

we enhance pathname to make our code more readable

Instance Method Summary collapse

Instance Method Details

#/(that) ⇒ Object



89
90
91
# File 'lib/extend/pathname.rb', line 89

def / that
  join that.to_s
end

#as_backupObject



93
94
95
# File 'lib/extend/pathname.rb', line 93

def as_backup
  Pathname.new File.join(dirname, "#{basename}.lace.bak")
end

#as_dotfile(base_folder, subtract) ⇒ Object



97
98
99
# 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

#cdObject



68
69
70
# File 'lib/extend/pathname.rb', line 68

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

#chmod_R(perms) ⇒ Object



59
60
61
62
# File 'lib/extend/pathname.rb', line 59

def chmod_R perms
  require 'fileutils'
  FileUtils.chmod_R perms, to_s
end

#cp(dst) ⇒ Object



28
29
30
31
32
33
34
35
# 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_pathObject



76
77
78
# File 'lib/extend/pathname.rb', line 76

def resolved_path
  self.symlink? ? dirname+readlink : self
end

#resolved_path_exists?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# 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_possibleObject

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

#stemObject

for filetypes we support, basename without extension



38
39
40
# File 'lib/extend/pathname.rb', line 38

def stem
  File.basename((path = to_s), extname(path))
end

#subdirsObject



72
73
74
# File 'lib/extend/pathname.rb', line 72

def subdirs
  children.select{ |child| child.directory? }
end