Skip to content

Commit 9135042

Browse files
author
rwh
committed
finish subdirs/filename rename
1 parent 9c7e071 commit 9135042

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

lib/compsci/unix_path.rb

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ module CompSci
2424
# In string form, directories are indicated with a trailing slash (/).
2525
# With no trailing slash, the last segment of a path is treated as a filename.
2626
#
27-
# Internally, there is a @filename string, and if it is empty, this has
27+
# Internally, there is a @file string, and if it is empty, this has
2828
# semantic meaning: the UnixPath is a directory.
2929

3030
# Basename and Extension
3131
# ===
3232
#
33-
# For nonempty @filename, it may be further broken down into basename
33+
# For nonempty @file, it may be further broken down into basename
3434
# and extension, with an overly simple rule:
3535
#
3636
# Look for the last dot.
@@ -44,14 +44,14 @@ module CompSci
4444
# Concatenating basename and extension will always perfectly reconstruct
4545
# the filename.
4646
#
47-
# If @filename is empty, basename and extension are nil.
47+
# If @file is empty, basename and extension are nil.
4848

4949
# Internal Representation
5050
# ===
5151
#
5252
# * abs: boolean indicating abspath vs relpath
53-
# * subdirs: array of strings, like %w[home user docs]
54-
# * filename: string, possibly empty
53+
# * dirs: array of strings, like %w[home user docs]
54+
# * file: string, possibly empty
5555

5656
# Strings
5757
# ===
@@ -72,7 +72,7 @@ module CompSci
7272
# * UnixPath.parse('/etc/systemd/system')
7373
#
7474
# Or construct by hand:
75-
# * UnixPath.new(abs: true, subdirs: %w[etc systemd system])
75+
# * UnixPath.new(abs: true, dirs: %w[etc systemd system])
7676

7777
# Implementation Details
7878
# ===
@@ -129,23 +129,23 @@ def parse(path_str)
129129
elsif path_str.start_with? CWD
130130
path_str = path_str[2..-1]
131131
end
132-
subdirs = path_str.split SEP
133-
filename = path_str.end_with?(SEP) ? '' : (subdirs.pop || '')
134-
self.new(abs: abs, subdirs: subdirs, filename: filename)
132+
dirs = path_str.split SEP
133+
file = path_str.end_with?(SEP) ? '' : (dirs.pop || '')
134+
self.new(abs: abs, dirs: dirs, file: file)
135135
end
136136

137137
# Create absolute Path from Path or string
138138
def absolute(path)
139139
p = parse(path.to_s)
140-
self.new(abs: true, subdirs: p.subdirs, filename: p.filename)
140+
self.new(abs: true, dirs: p.dirs, file: p.file)
141141
end
142142

143143
# Create directory Path from Path or string
144144
def directory(path)
145145
p = parse(path.to_s)
146-
subdirs = p.subdirs.dup
147-
subdirs << p.filename unless p.filename.empty?
148-
self.new(abs: p.abs, subdirs: subdirs, filename: '')
146+
dirs = p.dirs.dup
147+
dirs << p.file unless p.file.empty?
148+
self.new(abs: p.abs, dirs: dirs, file: '')
149149
end
150150
end
151151

@@ -158,28 +158,28 @@ def self.included(base) = base.extend(FactoryMethods)
158158

159159
# a pseudo-initialize method to be called by the base class #initialize
160160
# returns a hash
161-
def handle_init(abs: false, subdirs: [], filename: '')
162-
PathMixin.valid_filename!(filename)
161+
def handle_init(abs: false, dirs: [], file: '')
162+
PathMixin.valid_filename!(file)
163163
{ abs: abs,
164-
subdirs: subdirs.reject { |dir| dir == DOT },
165-
filename: filename }
164+
dirs: dirs.reject { |d| d == DOT },
165+
file: file }
166166
end
167167

168168
def sigil = self.abs ? SEP : CWD
169169
def path
170-
self.subdirs.empty? ? self.filename :
171-
(self.subdirs + [self.filename]).join(SEP)
170+
self.dirs.empty? ? self.file :
171+
(self.dirs + [self.file]).join(SEP)
172172
end
173173
def to_s = self.sigil + self.path
174174

175175
def abs? = self.abs
176-
def dir? = self.filename.empty?
176+
def dir? = self.file.empty?
177177

178178
def ==(other)
179179
other.is_a?(self.class) and
180180
self.abs == other.abs and
181-
self.subdirs == other.subdirs and
182-
self.filename == other.filename
181+
self.dirs == other.dirs and
182+
self.file == other.file
183183
end
184184

185185
# Comparable: lexicographic -- relpaths before abspaths
@@ -201,32 +201,32 @@ def slash(other)
201201
end
202202
alias_method :/, :slash
203203

204-
# whatever the filename is, up to the last dot; possibly empty: e.g. .bashrc
204+
# whatever the file is, up to the last dot; possibly empty: e.g. .bashrc
205205
def basename
206-
unless self.filename.empty?
207-
idx = self.filename.rindex(DOT)
208-
idx.nil? ? self.filename : self.filename[0...idx]
206+
unless self.file.empty?
207+
idx = self.file.rindex(DOT)
208+
idx.nil? ? self.file : self.file[0...idx]
209209
end
210210
end
211211

212212
# include the dot; empty if there is no extension: e.g. /bin/bash
213213
def extension
214-
unless self.filename.empty?
215-
idx = self.filename.rindex(DOT)
216-
idx.nil? ? "" : self.filename[idx..-1]
214+
unless self.file.empty?
215+
idx = self.file.rindex(DOT)
216+
idx.nil? ? "" : self.file[idx..-1]
217217
end
218218
end
219219

220220
# ocean boiler; don't use this
221-
def reconstruct_filename = [self.basename, self.extension].join
221+
def reconstruct_file = [self.basename, self.extension].join
222222
end
223223

224224

225225
#
226226
# ImmutablePath (Data)
227227
#
228228

229-
class ImmutablePath < Data.define(:abs, :subdirs, :filename)
229+
class ImmutablePath < Data.define(:abs, :dirs, :file)
230230
include PathMixin # also extends PathMixin::FactoryMethods
231231

232232
# rely on PathMixin#handle_init for initialize
@@ -236,35 +236,35 @@ def initialize(**kwargs)
236236

237237
# rely on Data#with for efficient copying
238238
def slash_path(other)
239-
# nonempty filename is now a subdir
240-
dirs = self.subdirs.dup # dup first
241-
dirs << self.filename unless self.filename.empty?
242-
dirs += other.subdirs
243-
self.with(subdirs: dirs, filename: other.filename)
239+
# nonempty file is now a subdir
240+
new_dirs = self.dirs.dup # dup first
241+
new_dirs << self.file unless self.file.empty?
242+
new_dirs += other.dirs
243+
self.with(dirs: new_dirs, file: other.file)
244244
end
245245
end
246246

247247
class MutablePath
248248
include PathMixin # also extends PathMixin::FactoryMethods
249249

250-
attr_reader :abs, :subdirs, :filename
250+
attr_reader :abs, :dirs, :file
251251

252252
# rely on PathMixin#handle_init for initialize
253253
def initialize(**kwargs)
254-
@abs, @subdirs, @filename =
255-
self.handle_init(**kwargs).values_at(:abs, :subdirs, :filename)
254+
@abs, @dirs, @file =
255+
self.handle_init(**kwargs).values_at(:abs, :dirs, :file)
256256
end
257257

258-
# enforce valid mutable filenames
259-
def filename=(val)
260-
@filename = PathMixin.valid_filename!(val)
258+
# enforce valid mutable files
259+
def file=(val)
260+
@file = PathMixin.valid_filename!(val)
261261
end
262262

263263
# simple update
264264
def slash_path(other)
265-
@subdirs << @filename unless @filename.empty?
266-
@subdirs += other.subdirs
267-
@filename = other.filename
265+
@dirs << @file unless @file.empty?
266+
@dirs += other.dirs
267+
@file = other.file
268268
self
269269
end
270270
end

0 commit comments

Comments
 (0)