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



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

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



74
75
76
# File 'lib/quality_extensions/pathname.rb', line 74

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



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

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

#add_prefix(s) ⇒ Object



181
182
183
# File 'lib/quality_extensions/pathname.rb', line 181

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’)



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

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.



132
133
134
135
# File 'lib/quality_extensions/pathname.rb', line 132

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.



144
145
146
147
# File 'lib/quality_extensions/pathname.rb', line 144

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?



297
298
299
300
301
302
# File 'lib/quality_extensions/pathname.rb', line 297

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.



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

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.



120
121
122
123
# File 'lib/quality_extensions/pathname.rb', line 120

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.)



227
228
229
230
231
# File 'lib/quality_extensions/pathname.rb', line 227

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()?



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/quality_extensions/pathname.rb', line 191

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()?



250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/quality_extensions/pathname.rb', line 250

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.



209
210
211
212
213
# File 'lib/quality_extensions/pathname.rb', line 209

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’

Returns self. This is different from FileUtils.touch, which returns an array of filenames.



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

def touch
  tap {|file| FileUtils.touch file.to_s }
end