class FSPath
Extension of Pathname with helpful methods and fixes
Public Class Methods
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
Fixing getwd
# File lib/fspath.rb, line 253 def self.getwd new(super) end
Fixing glob
# 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
Fixing pwd
# File lib/fspath.rb, line 258 def self.pwd new(super) end
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
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
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
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
# 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
Fixing Pathname#+
# File lib/fspath.rb, line 115 def +(other) self.class.new(super) end
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 to file
# File lib/fspath.rb, line 171 def append(data) open('a') do |f| f.write(data) end end
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
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
Fixing basename
# File lib/fspath.rb, line 263 def basename(*args) self.class.new(super) end
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
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
Write data to file opened in binary mode
# File lib/fspath.rb, line 165 def binwrite(data, offset = nil) _write(data, offset, true) end
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
Returns list of elements in the given path in descending order
# File lib/fspath.rb, line 212 def descendants ascendants.reverse end
Fixing dirname
# File lib/fspath.rb, line 268 def dirname self.class.new(super) end
Fixing each_entry
# File lib/fspath.rb, line 312 def each_entry super do |f| yield self.class.new(f) end end
Fixing entries
# File lib/fspath.rb, line 319 def entries super.map{ |f| self.class.new(f) } end
Escape characters in glob pattern
# File lib/fspath.rb, line 185 def escape_glob self.class.new(escape_glob_string) end
Fixing expand_path
# File lib/fspath.rb, line 273 def expand_path(*args) self.class.new(super) end
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
Fixing inspect
# File lib/fspath.rb, line 326 def inspect "#<#{self.class}:#{@path}>" end
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
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
Fixing readlink
# File lib/fspath.rb, line 307 def readlink self.class.new(super) end
Fixing realdirpath
# File lib/fspath.rb, line 301 def realdirpath self.class.new(super) end
Fixing realpath
# File lib/fspath.rb, line 295 def realpath self.class.new(super) end
Fixing Pathname.relative_path_from
# File lib/fspath.rb, line 142 def relative_path_from(other) self.class.new(super(self.class.new(other))) end
Fixing split
# File lib/fspath.rb, line 278 def split super.map{ |f| self.class.new(f) } end
Fixing sub
# File lib/fspath.rb, line 283 def sub(pattern, *rest, &block) self.class.new(super) end
Fixing sub_ext
# File lib/fspath.rb, line 289 def sub_ext(ext) self.class.new(super) end
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
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
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 to file
# File lib/fspath.rb, line 158 def write(data, offset = nil) _write(data, offset, false) end
Private Instance Methods
# 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
# File lib/fspath.rb, line 333 def escape_glob_string @path.gsub(/([\*\?\[\]\{\}])/, '\\\\\1') end