class FSPath

Extension of Pathname with helpful methods and fixes

Public Class Methods

common_dir(*paths) click to toggle source

Returns common dir for paths

# File lib/fspath.rb, line 52
def common_dir(*paths)
  fail ArgumentError, 'At least one path is required' if paths.empty?

  paths.map do |path|
    new(path).dirname.ascendants
  end.inject(:&).first
end
getwd() click to toggle source

Fixing getwd

Calls superclass method
# File lib/fspath.rb, line 253
def self.getwd
  new(super)
end
glob(*args) { |new(f)| ... } click to toggle source

Fixing glob

Calls superclass method
# File lib/fspath.rb, line 242
def self.glob(*args)
  if block_given?
    super do |f|
      yield new(f)
    end
  else
    super.map{ |f| new(f) }
  end
end
pwd() click to toggle source

Fixing pwd

Calls superclass method
# File lib/fspath.rb, line 258
def self.pwd
  new(super)
end
temp_dir(prefix_suffix = nil, *args) { |new(dir)| ... } click to toggle source

Returns or yields FSPath with temp directory created by Dir.mktmpdir

# File lib/fspath.rb, line 86
def temp_dir(prefix_suffix = nil, *args)
  prefix_suffix = fix_prefix_suffix(prefix_suffix || 'd')
  if block_given?
    Dir.mktmpdir(prefix_suffix, *args) do |dir|
      yield new(dir)
    end
  else
    new(Dir.mktmpdir(prefix_suffix, *args))
  end
end
temp_file(prefix_suffix = nil, *args, &block) click to toggle source

Returns or yields temp file created by Tempfile.new with path returning FSPath

# File lib/fspath.rb, line 62
def temp_file(prefix_suffix = nil, *args, &block)
  prefix_suffix = fix_prefix_suffix(prefix_suffix || 'f')
  Tempfile.open(self, prefix_suffix, *args, &block)
end
temp_file_path(prefix_suffix = nil, *args) { |path| ... } click to toggle source

Returns or yields path as FSPath of temp file created by Tempfile.new WARNING: loosing reference to returned object will remove file on nearest GC run

# File lib/fspath.rb, line 70
def temp_file_path(prefix_suffix = nil, *args)
  if block_given?
    temp_file(prefix_suffix, *args) do |file|
      file.close
      yield file.path
    end
  else
    file = temp_file(prefix_suffix, *args)
    file.close
    path = file.path
    path.instance_variable_set(:@__temp_file, file)
    path
  end
end
~(name = nil) click to toggle source

Return current user home path if called without argument. If called with argument return specified user home path.

# File lib/fspath.rb, line 47
def ~(name = nil)
  new(File.expand_path("~#{name}"))
end

Private Class Methods

fix_prefix_suffix(prefix_suffix) click to toggle source
# File lib/fspath.rb, line 99
def fix_prefix_suffix(prefix_suffix)
  if prefix_suffix.is_a?(Array)
    prefix_suffix.map(&:to_s)
  else
    prefix_suffix.to_s
  end
end

Public Instance Methods

+(other) click to toggle source

Fixing Pathname#+

Calls superclass method
# File lib/fspath.rb, line 115
def +(other)
  self.class.new(super)
end
/(other) click to toggle source

Join paths using File.join

# File lib/fspath.rb, line 109
def /(other)
  self.class.new(File.join(@path, other.to_s))
end
append(data) click to toggle source

Append data to file

# File lib/fspath.rb, line 171
def append(data)
  open('a') do |f|
    f.write(data)
  end
end
ascend(&block) click to toggle source

Iterates over and yields each element in the given path in ascending order

# File lib/fspath.rb, line 217
def ascend(&block)
  paths = ascendants
  paths.each(&block) if block
  paths
end
ascendants() click to toggle source

Returns list of elements in the given path in ascending order

# File lib/fspath.rb, line 198
def ascendants
  paths = []
  path = @path
  paths << self
  while (r = chop_basename(path))
    path = r.first
    break if path.empty?

    paths << self.class.new(del_trailing_separator(path))
  end
  paths
end
basename(*args) click to toggle source

Fixing basename

Calls superclass method
# File lib/fspath.rb, line 263
def basename(*args)
  self.class.new(super)
end
binappend(data) click to toggle source

Append data to file opened in binary mode

# File lib/fspath.rb, line 178
def binappend(data)
  open('ab') do |f|
    f.write(data)
  end
end
binread(length = nil, offset = nil) click to toggle source

Read data from file opened in binary mode

# File lib/fspath.rb, line 148
def binread(length = nil, offset = nil)
  open('rb') do |f|
    f.seek(offset) if offset
    f.read(length)
  end
end
binwrite(data, offset = nil) click to toggle source

Write data to file opened in binary mode

# File lib/fspath.rb, line 165
def binwrite(data, offset = nil)
  _write(data, offset, true)
end
descend(&block) click to toggle source

Iterates over and yields each element in the given path in descending order

# File lib/fspath.rb, line 224
def descend(&block)
  paths = descendants
  paths.each(&block) if block
  paths
end
descendants() click to toggle source

Returns list of elements in the given path in descending order

# File lib/fspath.rb, line 212
def descendants
  ascendants.reverse
end
dirname() click to toggle source

Fixing dirname

Calls superclass method
# File lib/fspath.rb, line 268
def dirname
  self.class.new(super)
end
each_entry() { |class.new(f)| ... } click to toggle source

Fixing each_entry

Calls superclass method
# File lib/fspath.rb, line 312
def each_entry
  super do |f|
    yield self.class.new(f)
  end
end
entries() click to toggle source

Fixing entries

Calls superclass method
# File lib/fspath.rb, line 319
def entries
  super.map{ |f| self.class.new(f) }
end
escape_glob() click to toggle source

Escape characters in glob pattern

# File lib/fspath.rb, line 185
def escape_glob
  self.class.new(escape_glob_string)
end
expand_path(*args) click to toggle source

Fixing expand_path

Calls superclass method
# File lib/fspath.rb, line 273
def expand_path(*args)
  self.class.new(super)
end
glob(*args, &block) click to toggle source

Expand glob

# File lib/fspath.rb, line 190
def glob(*args, &block)
  flags = args.last.is_a?(Integer) ? args.pop : nil
  args = [File.join(escape_glob_string, *args)]
  args << flags if flags
  self.class.glob(*args, &block)
end
inspect() click to toggle source

Fixing inspect

# File lib/fspath.rb, line 326
def inspect
  "#<#{self.class}:#{@path}>"
end
parts() click to toggle source

Returns path parts

FSPath('/a/b/c').parts    # ['/', 'a', 'b', 'c']
FSPath('a/b/c').parts     # ['a', 'b', 'c']
FSPath('./a/b/c').parts   # ['.', 'a', 'b', 'c']
FSPath('a/../b/c').parts  # ['a', '..', 'b', 'c']
# File lib/fspath.rb, line 235
def parts
  prefix, parts = split_names(@path)
  prefix.empty? ? parts : [prefix] + parts
end
prefix_suffix() click to toggle source

Return basename without ext and ext as two-element array

# File lib/fspath.rb, line 121
def prefix_suffix
  ext = extname
  [basename(ext), ext]
end
realdirpath() click to toggle source

Fixing realdirpath

Calls superclass method
# File lib/fspath.rb, line 301
def realdirpath
  self.class.new(super)
end
realpath() click to toggle source

Fixing realpath

Calls superclass method
# File lib/fspath.rb, line 295
def realpath
  self.class.new(super)
end
relative_path_from(other) click to toggle source

Fixing Pathname.relative_path_from

Calls superclass method
# File lib/fspath.rb, line 142
def relative_path_from(other)
  self.class.new(super(self.class.new(other)))
end
split() click to toggle source

Fixing split

Calls superclass method
# File lib/fspath.rb, line 278
def split
  super.map{ |f| self.class.new(f) }
end
sub(pattern, *rest, &block) click to toggle source

Fixing sub

Calls superclass method
# File lib/fspath.rb, line 283
def sub(pattern, *rest, &block)
  self.class.new(super)
end
sub_ext(ext) click to toggle source

Fixing sub_ext

Calls superclass method
# File lib/fspath.rb, line 289
def sub_ext(ext)
  self.class.new(super)
end
temp_dir(*args, &block) click to toggle source

Calls class method with prefix and suffix set using prefix_suffix

# File lib/fspath.rb, line 137
def temp_dir(*args, &block)
  self.class.temp_dir(prefix_suffix, *args, &block)
end
temp_file(*args, &block) click to toggle source

Calls class method with prefix and suffix set using prefix_suffix

# File lib/fspath.rb, line 127
def temp_file(*args, &block)
  self.class.temp_file(prefix_suffix, *args, &block)
end
temp_file_path(*args, &block) click to toggle source

Calls class method with prefix and suffix set using prefix_suffix

# File lib/fspath.rb, line 132
def temp_file_path(*args, &block)
  self.class.temp_file_path(prefix_suffix, *args, &block)
end
write(data, offset = nil) click to toggle source

Write data to file

# File lib/fspath.rb, line 158
def write(data, offset = nil)
  _write(data, offset, false)
end

Private Instance Methods

_write(data, offset, binmode) click to toggle source
# File lib/fspath.rb, line 337
def _write(data, offset, binmode)
  mode = if offset
    Fcntl::O_CREAT | Fcntl::O_RDWR # fix for jruby 1.8 truncating with WRONLY
  else
    Fcntl::O_CREAT | Fcntl::O_WRONLY | Fcntl::O_TRUNC
  end

  File.open(@path, mode) do |f|
    f.binmode if binmode
    f.seek(offset) if offset
    f.write(data)
  end
end
escape_glob_string() click to toggle source
# File lib/fspath.rb, line 333
def escape_glob_string
  @path.gsub(/([\*\?\[\]\{\}])/, '\\\\\1')
end