Class: FSPath

Inherits:
Pathname
  • Object
show all
Defined in:
lib/fspath.rb

Overview

Extension of Pathname with helpful methods and fixes

Defined Under Namespace

Classes: Tempfile

Class Method Summary collapse

Instance Method Summary collapse

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

.getwdObject

Fixing getwd



216
217
218
# File 'lib/fspath.rb', line 216

def self.getwd
  new(super)
end

.glob(*args) ⇒ Object

Fixing glob



205
206
207
208
209
210
211
212
213
# File 'lib/fspath.rb', line 205

def self.glob(*args)
  if block_given?
    super do |f|
      yield new(f)
    end
  else
    super.map{ |f| new(f) }
  end
end

.pwdObject

Fixing pwd



221
222
223
# File 'lib/fspath.rb', line 221

def self.pwd
  new(super)
end

.temp_dir(*args) ⇒ Object

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



82
83
84
85
86
87
88
89
90
# File 'lib/fspath.rb', line 82

def temp_dir(*args)
  if block_given?
    Dir.mktmpdir(*args) do |dir|
      yield new(dir)
    end
  else
    new(Dir.mktmpdir(*args))
  end
end

.temp_file(*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(*args, &block)
  args = %w[f] if args.empty?
  Tempfile.open(self, *args, &block)
end

.temp_file_path(*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(*args)
  if block_given?
    temp_file(*args) do |file|
      file.close
      yield file.path
    end
  else
    file = temp_file(*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.expand_path("~#{name}"))
end

Instance Method Details

#+(other) ⇒ Object

Fixing Pathname#+



100
101
102
# File 'lib/fspath.rb', line 100

def +(other)
  self.class.new(super)
end

#/(other) ⇒ Object

Join paths using File.join



94
95
96
# File 'lib/fspath.rb', line 94

def /(other)
  self.class.new(File.join(@path, other.to_s))
end

#append(data) ⇒ Object

Append data to file



135
136
137
138
139
# File 'lib/fspath.rb', line 135

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



180
181
182
183
184
# File 'lib/fspath.rb', line 180

def ascend(&block)
  paths = ascendants
  paths.each(&block) if block
  paths
end

#ascendantsObject

Returns list of elements in the given path in ascending order



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/fspath.rb', line 162

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



226
227
228
# File 'lib/fspath.rb', line 226

def basename(*args)
  self.class.new(super)
end

#binappend(data) ⇒ Object

Append data to file opened in binary mode



142
143
144
145
146
# File 'lib/fspath.rb', line 142

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



112
113
114
115
116
117
# File 'lib/fspath.rb', line 112

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



129
130
131
# File 'lib/fspath.rb', line 129

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



187
188
189
190
191
# File 'lib/fspath.rb', line 187

def descend(&block)
  paths = descendants
  paths.each(&block) if block
  paths
end

#descendantsObject

Returns list of elements in the given path in descending order



175
176
177
# File 'lib/fspath.rb', line 175

def descendants
  ascendants.reverse
end

#dirnameObject

Fixing dirname



231
232
233
# File 'lib/fspath.rb', line 231

def dirname
  self.class.new(super)
end

#each_entryObject

Fixing each_entry



275
276
277
278
279
# File 'lib/fspath.rb', line 275

def each_entry
  super do |f|
    yield self.class.new(f)
  end
end

#entriesObject

Fixing entries



282
283
284
# File 'lib/fspath.rb', line 282

def entries
  super.map{ |f| self.class.new(f) }
end

#escape_globObject

Escape characters in glob pattern



149
150
151
# File 'lib/fspath.rb', line 149

def escape_glob
  self.class.new(escape_glob_string)
end

#expand_path(*args) ⇒ Object

Fixing expand_path



236
237
238
# File 'lib/fspath.rb', line 236

def expand_path(*args)
  self.class.new(super)
end

#glob(*args, &block) ⇒ Object

Expand glob



154
155
156
157
158
159
# File 'lib/fspath.rb', line 154

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

#inspectObject

Fixing inspect



289
290
291
# File 'lib/fspath.rb', line 289

def inspect
  "#<#{self.class}:#{@path}>"
end

#partsObject

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']


198
199
200
201
# File 'lib/fspath.rb', line 198

def parts
  prefix, parts = split_names(@path)
  prefix.empty? ? parts : [prefix] + parts
end

Fixing readlink



270
271
272
# File 'lib/fspath.rb', line 270

def readlink
  self.class.new(super)
end

#realdirpathObject

Fixing realdirpath



264
265
266
# File 'lib/fspath.rb', line 264

def realdirpath
  self.class.new(super)
end

#realpathObject

Fixing realpath



258
259
260
# File 'lib/fspath.rb', line 258

def realpath
  self.class.new(super)
end

#relative_path_from(other) ⇒ Object

Fixing Pathname.relative_path_from



106
107
108
# File 'lib/fspath.rb', line 106

def relative_path_from(other)
  self.class.new(super(self.class.new(other)))
end

#splitObject

Fixing split



241
242
243
# File 'lib/fspath.rb', line 241

def split
  super.map{ |f| self.class.new(f) }
end

#sub(pattern, *rest, &block) ⇒ Object

Fixing sub



246
247
248
# File 'lib/fspath.rb', line 246

def sub(pattern, *rest, &block)
  self.class.new(super)
end

#sub_ext(ext) ⇒ Object

Fixing sub_ext



252
253
254
# File 'lib/fspath.rb', line 252

def sub_ext(ext)
  self.class.new(super)
end

#write(data, offset = nil) ⇒ Object

Write data to file



122
123
124
# File 'lib/fspath.rb', line 122

def write(data, offset = nil)
  _write(data, offset, false)
end