Class: Pathname

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.getwdObject

Pathname.new(Dir.getwd)

Name ideas: cwd



33
34
35
# File 'lib/quality_extensions/pathname.rb', line 33

def self.getwd
  Pathname.new(Dir.getwd)
end

.tempfileObject

Creates a new temp file using Tempfile.new and returns the Pathname object for that file



26
27
28
# File 'lib/quality_extensions/pathname.rb', line 26

def self.tempfile
  Pathname.new(Tempfile.new('Pathname').path)
end

Instance Method Details

#absolutizeObject

Returns a Pathname object representing the absolute path equivalent to self.

If self is already absolute, returns self. Otherwise, prepends Pathname.getwd (in other words, creates an absolute path relative to the current working directory.)

Pathname.new(‘.’).absolutize == Pathname.getwd # => true



43
44
45
46
47
48
49
# File 'lib/quality_extensions/pathname.rb', line 43

def absolutize
  if absolute?
    self
  else
    (Pathname.getwd + self).cleanpath
  end
end

#add_prefix(s) ⇒ Object



130
131
132
# File 'lib/quality_extensions/pathname.rb', line 130

def add_prefix(s)
  Pathname.new((dirname + s).to_s + basename.to_s)
end

#add_suffix(s) ⇒ Object

This is needed since <#Pathname> + <#String> treats both self and the string as a directory to be joined instead of simply treating them as parts of a basename/filename to join together.

Pathname.new(‘/tmp/some_file’) + ‘.suffix’

> Pathname.new(‘/tmp/some_file/.suffix’)

Pathname.new(‘/tmp/some_file’).add_suffix(‘.suffix’)

> Pathname.new(‘/tmp/some_file.suffix’)

Pathname.new(‘/tmp/some_file/’).add_suffix(‘.suffix’)

> Pathname.new(‘/tmp/some_file.suffix’)



126
127
128
# File 'lib/quality_extensions/pathname.rb', line 126

def add_suffix(s)
  Pathname.new(cleanpath.to_s + s)
end

#cp(dest, options = {}) ⇒ Object Also known as: copy

Copies self to dest using FileUtils.cp.

See documentation for FileUtils.cp for a list of valid options.

Returns Pathname object for dest file.



81
82
83
84
# File 'lib/quality_extensions/pathname.rb', line 81

def cp(dest, options = {})
  FileUtils.cp self.to_s, dest.to_s, options
  Pathname.new(dest)
end

#cp_r(dest, options = {}) ⇒ Object Also known as: copy_recursive

Copies self to dest using FileUtils.cp_r. If self is a directory, this method copies all its contents recursively. If dest is a directory, copies self to dest/src.

See documentation for FileUtils.cp_r for a list of valid options.

Returns Pathname object for dest file.



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

def cp_r(dest, options = {})
  FileUtils.cp_r self.to_s, dest.to_s, options
  Pathname.new(dest)
end

#each_parent_dirObject

Traverses up the file system until a match is found that is described by block (that is, until block yields a true value).

Yields each parent directory (including self if self.directory?) as a Pathname object, one at a time, until we get to root of the file system (absolutize is called to force it to be an absolute path) or the block indicates that it has found what it’s looking for.

find_ancestor will return as its return value the block’s return value as soon as the block’s return value evaluates to true (not nil or false). If no match is found, find_ancestor will return nil.

Examples:

git_dir = Pathname.getwd.each_parent_dir {|dir| path = dir + '.git'; break path if path.exist? }
git_dirs = []; Pathname.getwd.each_parent_dir {|dir| path = dir + '.git'; git_dirs << path if path.exist? }

To do: deprecate, merge docs with parent_dirs?



246
247
248
249
250
251
# File 'lib/quality_extensions/pathname.rb', line 246

def each_parent_dir
  parent_dirs.reverse.each do |part|
    ret = yield part
  end
  nil
end

#install(dest, options = {}) ⇒ Object

Copies/install self to dest using FileUtils.install.

If src is not same as dest, copies it and changes the permission mode to mode. If dest is a directory, destination is dest/src.

FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true

Returns Pathname object for dest file.



109
110
111
112
# File 'lib/quality_extensions/pathname.rb', line 109

def install(dest, options = {})
  FileUtils.install self.to_s, dest.to_s, options
  Pathname.new(dest)
end

#mv(new_path, options = {}) ⇒ Object Also known as: move

Moves self to new_path using FileUtils.mv.

See documentation for FileUtils.mv for a list of valid options.

Returns Pathname object for new_file file.



69
70
71
72
# File 'lib/quality_extensions/pathname.rb', line 69

def mv(new_path, options = {})
  FileUtils.mv self.to_s, new_path.to_s, options
  Pathname.new(new_path)
end

#parent_dirs(include_self = true) ⇒ Object

Returns an array of all path parts that are directories. (If self.directory?, self is included too, unless include_self is false.)

This is similar to parts_without_basename except unlike parts_without_basename, which removes the last path (‘basename’) part no matter what, parent_dirs actually checks if the last path part is a directory and only removes it if it is not.

absolutize is called to force it to be an absolute path; so this method will not behave as advertized if the path is invalid or if the current working directory isn’t correct when the path is absolutized…

parent_dirs is not useful when used with fictional paths. It actually calls #directory? on the last path part, so the path must actually exist for this method to work as advertised. (You would might be advised to check #exist? before calling this method.) If you are confident that self is a directory, then you might want to use parts_without_basename.

Name ideas: parents, ancestors (but since it also includes self by default, I thought emphasizing dirs would be less misleading.)



176
177
178
179
180
# File 'lib/quality_extensions/pathname.rb', line 176

def parent_dirs(include_self = true)
  parents = absolutize.split_all.dup
  parents.pop if parents.any? && !(parents.last.directory? && include_self)
  parents
end

#split_allObject

Better name? Would ‘dirs’ be better? ‘parents’?

Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory/basename represented in the path.

Similar to PHP’s pathinfo()?



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/quality_extensions/pathname.rb', line 140

def split_all
  # Boundary condition for paths like '/usr' (['/', 'usr'] will be the result)
  if self.to_s == '/'
    [self]
  # Boundary condition for paths like 'usr' (we only want ['usr'], not ['.', 'usr'])
  elsif self.to_s == '.'
    []
  else
    parent.split_all + [self]
  end
end

#split_all_sObject

Better name? Would ‘dirs’ be better?

Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory represented in the path.

Pathname.new(‘dir1/dir2/base’).parts_s # => [‘dir1’, ‘dir2’, ‘base’]

Pathname.new(‘dir1/dir2/base’).parts_s.first # => ‘dir1’

Pathname.new(‘/dir1/dir2/base’).parts_s # => [‘/’, ‘dir1’, ‘dir2’, ‘base’]

Unlike split_all, this returns an array of strings rather than an array of Pathnames.

Similar to PHP’s pathinfo()?



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/quality_extensions/pathname.rb', line 199

def split_all_s
  a, b = split
  a, b = a, b.to_s

  # Boundary condition for paths like '/usr' (['/', 'usr'] will be the result)
  if b == '/'
    [b]
  # Boundary condition for paths like 'usr' (we only want ['usr'], not ['.', 'usr'])
  elsif b == '.'
    []
  else
    a.split_all_s + [b]
  end
end

#split_all_without_basenameObject

Returns all path parts except the last (the last part being the basename).

When there is only one part (as is the case, f.e., with Pathname(‘/’) or Pathname(‘file’)), it returns an empty array (rather than Pathname(‘/’) or Pathname(‘.’)).

Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory represented in the path.



158
159
160
161
162
# File 'lib/quality_extensions/pathname.rb', line 158

def split_all_without_basename
  parents = split_all.dup
  parents.pop
  parents
end

#touchObject

Same as FileUtils.touch

Options: noop verbose

Updates modification time (mtime) and access time (atime) of file(s) in list. Files are created if they don’t exist.

FileUtils.touch ‘timestamp’ FileUtils.touch Dir.glob(‘*.c’); system ‘make’



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

def touch
  FileUtils.touch self.to_s
end