Class: FSPath
- Inherits:
-
Pathname
- Object
- Pathname
- FSPath
- Defined in:
- lib/fspath.rb
Overview
Extension of Pathname with helpful methods and fixes
Defined Under Namespace
Classes: Tempfile
Class Method Summary collapse
-
.common_dir(*paths) ⇒ Object
Returns common dir for paths.
-
.getwd ⇒ Object
Fixing getwd.
-
.glob(*args) ⇒ Object
Fixing glob.
-
.pwd ⇒ Object
Fixing pwd.
-
.temp_dir(prefix_suffix = nil, *args) ⇒ Object
Returns or yields FSPath with temp directory created by Dir.mktmpdir.
-
.temp_file(prefix_suffix = nil, *args, &block) ⇒ Object
Returns or yields temp file created by Tempfile.new with path returning FSPath.
-
.temp_file_path(prefix_suffix = nil, *args) ⇒ Object
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.
-
.~(name = nil) ⇒ Object
Return current user home path if called without argument.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Fixing Pathname#+.
-
#/(other) ⇒ Object
Join paths using File.join.
-
#append(data) ⇒ Object
Append data to file.
-
#ascend(&block) ⇒ Object
Iterates over and yields each element in the given path in ascending order.
-
#ascendants ⇒ Object
Returns list of elements in the given path in ascending order.
-
#basename(*args) ⇒ Object
Fixing basename.
-
#binappend(data) ⇒ Object
Append data to file opened in binary mode.
-
#binread(length = nil, offset = nil) ⇒ Object
Read data from file opened in binary mode.
-
#binwrite(data, offset = nil) ⇒ Object
Write data to file opened in binary mode.
-
#descend(&block) ⇒ Object
Iterates over and yields each element in the given path in descending order.
-
#descendants ⇒ Object
Returns list of elements in the given path in descending order.
-
#dirname ⇒ Object
Fixing dirname.
-
#each_entry ⇒ Object
Fixing each_entry.
-
#entries ⇒ Object
Fixing entries.
-
#escape_glob ⇒ Object
Escape characters in glob pattern.
-
#expand_path(*args) ⇒ Object
Fixing expand_path.
-
#glob(*args, &block) ⇒ Object
Expand glob.
-
#inspect ⇒ Object
Fixing inspect.
-
#parts ⇒ Object
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’].
-
#prefix_suffix ⇒ Object
Return basename without ext and ext as two-element array.
-
#readlink ⇒ Object
Fixing readlink.
-
#realdirpath ⇒ Object
Fixing realdirpath.
-
#realpath ⇒ Object
Fixing realpath.
-
#relative_path_from(other) ⇒ Object
Fixing Pathname.relative_path_from.
-
#split ⇒ Object
Fixing split.
-
#sub(pattern, *rest, &block) ⇒ Object
Fixing sub.
-
#sub_ext(ext) ⇒ Object
Fixing sub_ext.
-
#temp_dir(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix.
-
#temp_file(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix.
-
#temp_file_path(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix.
-
#write(data, offset = nil) ⇒ Object
Write data to file.
Class Method Details
.common_dir(*paths) ⇒ Object
Returns common dir for paths
49 50 51 52 53 54 |
# File 'lib/fspath.rb', line 49 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 ⇒ Object
Fixing getwd
248 249 250 |
# File 'lib/fspath.rb', line 248 def self.getwd new(super) end |
.glob(*args) ⇒ Object
Fixing glob
237 238 239 240 241 242 243 244 245 |
# File 'lib/fspath.rb', line 237 def self.glob(*args) if block_given? super do |f| yield new(f) end else super.map{ |f| new(f) } end end |
.pwd ⇒ Object
Fixing pwd
253 254 255 |
# File 'lib/fspath.rb', line 253 def self.pwd new(super) end |
.temp_dir(prefix_suffix = nil, *args) ⇒ Object
Returns or yields FSPath with temp directory created by Dir.mktmpdir
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fspath.rb', line 82 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) ⇒ Object
Returns or yields temp file created by Tempfile.new with path returning FSPath
58 59 60 61 |
# File 'lib/fspath.rb', line 58 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) ⇒ Object
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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fspath.rb', line 66 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) ⇒ Object
Return current user home path if called without argument. If called with argument return specified user home path.
44 45 46 |
# File 'lib/fspath.rb', line 44 def ~(name = nil) new(File.("~#{name}")) end |
Instance Method Details
#+(other) ⇒ Object
Fixing Pathname#+
111 112 113 |
# File 'lib/fspath.rb', line 111 def +(other) self.class.new(super) end |
#/(other) ⇒ Object
Join paths using File.join
105 106 107 |
# File 'lib/fspath.rb', line 105 def /(other) self.class.new(File.join(@path, other.to_s)) end |
#append(data) ⇒ Object
Append data to file
167 168 169 170 171 |
# File 'lib/fspath.rb', line 167 def append(data) open('a') do |f| f.write(data) end end |
#ascend(&block) ⇒ Object
Iterates over and yields each element in the given path in ascending order
212 213 214 215 216 |
# File 'lib/fspath.rb', line 212 def ascend(&block) paths = ascendants paths.each(&block) if block paths end |
#ascendants ⇒ Object
Returns list of elements in the given path in ascending order
194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/fspath.rb', line 194 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) ⇒ Object
Fixing basename
258 259 260 |
# File 'lib/fspath.rb', line 258 def basename(*args) self.class.new(super) end |
#binappend(data) ⇒ Object
Append data to file opened in binary mode
174 175 176 177 178 |
# File 'lib/fspath.rb', line 174 def binappend(data) open('ab') do |f| f.write(data) end end |
#binread(length = nil, offset = nil) ⇒ Object
Read data from file opened in binary mode
144 145 146 147 148 149 |
# File 'lib/fspath.rb', line 144 def binread(length = nil, offset = nil) open('rb') do |f| f.seek(offset) if offset f.read(length) end end |
#binwrite(data, offset = nil) ⇒ Object
Write data to file opened in binary mode
161 162 163 |
# File 'lib/fspath.rb', line 161 def binwrite(data, offset = nil) _write(data, offset, true) end |
#descend(&block) ⇒ Object
Iterates over and yields each element in the given path in descending order
219 220 221 222 223 |
# File 'lib/fspath.rb', line 219 def descend(&block) paths = descendants paths.each(&block) if block paths end |
#descendants ⇒ Object
Returns list of elements in the given path in descending order
207 208 209 |
# File 'lib/fspath.rb', line 207 def descendants ascendants.reverse end |
#dirname ⇒ Object
Fixing dirname
263 264 265 |
# File 'lib/fspath.rb', line 263 def dirname self.class.new(super) end |
#each_entry ⇒ Object
Fixing each_entry
307 308 309 310 311 |
# File 'lib/fspath.rb', line 307 def each_entry super do |f| yield self.class.new(f) end end |
#entries ⇒ Object
Fixing entries
314 315 316 |
# File 'lib/fspath.rb', line 314 def entries super.map{ |f| self.class.new(f) } end |
#escape_glob ⇒ Object
Escape characters in glob pattern
181 182 183 |
# File 'lib/fspath.rb', line 181 def escape_glob self.class.new(escape_glob_string) end |
#expand_path(*args) ⇒ Object
Fixing expand_path
268 269 270 |
# File 'lib/fspath.rb', line 268 def (*args) self.class.new(super) end |
#glob(*args, &block) ⇒ Object
Expand glob
186 187 188 189 190 191 |
# File 'lib/fspath.rb', line 186 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 ⇒ Object
Fixing inspect
321 322 323 |
# File 'lib/fspath.rb', line 321 def inspect "#<#{self.class}:#{@path}>" end |
#parts ⇒ Object
230 231 232 233 |
# File 'lib/fspath.rb', line 230 def parts prefix, parts = split_names(@path) prefix.empty? ? parts : [prefix] + parts end |
#prefix_suffix ⇒ Object
Return basename without ext and ext as two-element array
117 118 119 120 |
# File 'lib/fspath.rb', line 117 def prefix_suffix ext = extname [basename(ext), ext] end |
#readlink ⇒ Object
Fixing readlink
302 303 304 |
# File 'lib/fspath.rb', line 302 def readlink self.class.new(super) end |
#realdirpath ⇒ Object
Fixing realdirpath
296 297 298 |
# File 'lib/fspath.rb', line 296 def realdirpath self.class.new(super) end |
#realpath ⇒ Object
Fixing realpath
290 291 292 |
# File 'lib/fspath.rb', line 290 def realpath self.class.new(super) end |
#relative_path_from(other) ⇒ Object
Fixing Pathname.relative_path_from
138 139 140 |
# File 'lib/fspath.rb', line 138 def relative_path_from(other) self.class.new(super(self.class.new(other))) end |
#split ⇒ Object
Fixing split
273 274 275 |
# File 'lib/fspath.rb', line 273 def split super.map{ |f| self.class.new(f) } end |
#sub(pattern, *rest, &block) ⇒ Object
Fixing sub
278 279 280 |
# File 'lib/fspath.rb', line 278 def sub(pattern, *rest, &block) self.class.new(super) end |
#sub_ext(ext) ⇒ Object
Fixing sub_ext
284 285 286 |
# File 'lib/fspath.rb', line 284 def sub_ext(ext) self.class.new(super) end |
#temp_dir(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix
133 134 135 |
# File 'lib/fspath.rb', line 133 def temp_dir(*args, &block) self.class.temp_dir(prefix_suffix, *args, &block) end |
#temp_file(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix
123 124 125 |
# File 'lib/fspath.rb', line 123 def temp_file(*args, &block) self.class.temp_file(prefix_suffix, *args, &block) end |
#temp_file_path(*args, &block) ⇒ Object
Calls class method with prefix and suffix set using prefix_suffix
128 129 130 |
# File 'lib/fspath.rb', line 128 def temp_file_path(*args, &block) self.class.temp_file_path(prefix_suffix, *args, &block) end |
#write(data, offset = nil) ⇒ Object
Write data to file
154 155 156 |
# File 'lib/fspath.rb', line 154 def write(data, offset = nil) _write(data, offset, false) end |