@@ -24,13 +24,13 @@ module CompSci
24
24
# In string form, directories are indicated with a trailing slash (/).
25
25
# With no trailing slash, the last segment of a path is treated as a filename.
26
26
#
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
28
28
# semantic meaning: the UnixPath is a directory.
29
29
30
30
# Basename and Extension
31
31
# ===
32
32
#
33
- # For nonempty @filename , it may be further broken down into basename
33
+ # For nonempty @file , it may be further broken down into basename
34
34
# and extension, with an overly simple rule:
35
35
#
36
36
# Look for the last dot.
@@ -44,14 +44,14 @@ module CompSci
44
44
# Concatenating basename and extension will always perfectly reconstruct
45
45
# the filename.
46
46
#
47
- # If @filename is empty, basename and extension are nil.
47
+ # If @file is empty, basename and extension are nil.
48
48
49
49
# Internal Representation
50
50
# ===
51
51
#
52
52
# * 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
55
55
56
56
# Strings
57
57
# ===
@@ -72,7 +72,7 @@ module CompSci
72
72
# * UnixPath.parse('/etc/systemd/system')
73
73
#
74
74
# Or construct by hand:
75
- # * UnixPath.new(abs: true, subdirs : %w[etc systemd system])
75
+ # * UnixPath.new(abs: true, dirs : %w[etc systemd system])
76
76
77
77
# Implementation Details
78
78
# ===
@@ -129,23 +129,23 @@ def parse(path_str)
129
129
elsif path_str . start_with? CWD
130
130
path_str = path_str [ 2 ..-1 ]
131
131
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 )
135
135
end
136
136
137
137
# Create absolute Path from Path or string
138
138
def absolute ( path )
139
139
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 )
141
141
end
142
142
143
143
# Create directory Path from Path or string
144
144
def directory ( path )
145
145
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 : '' )
149
149
end
150
150
end
151
151
@@ -158,28 +158,28 @@ def self.included(base) = base.extend(FactoryMethods)
158
158
159
159
# a pseudo-initialize method to be called by the base class #initialize
160
160
# 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 )
163
163
{ abs : abs ,
164
- subdirs : subdirs . reject { |dir | dir == DOT } ,
165
- filename : filename }
164
+ dirs : dirs . reject { |d | d == DOT } ,
165
+ file : file }
166
166
end
167
167
168
168
def sigil = self . abs ? SEP : CWD
169
169
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 )
172
172
end
173
173
def to_s = self . sigil + self . path
174
174
175
175
def abs? = self . abs
176
- def dir? = self . filename . empty?
176
+ def dir? = self . file . empty?
177
177
178
178
def ==( other )
179
179
other . is_a? ( self . class ) and
180
180
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
183
183
end
184
184
185
185
# Comparable: lexicographic -- relpaths before abspaths
@@ -201,32 +201,32 @@ def slash(other)
201
201
end
202
202
alias_method :/ , :slash
203
203
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
205
205
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 ]
209
209
end
210
210
end
211
211
212
212
# include the dot; empty if there is no extension: e.g. /bin/bash
213
213
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 ]
217
217
end
218
218
end
219
219
220
220
# ocean boiler; don't use this
221
- def reconstruct_filename = [ self . basename , self . extension ] . join
221
+ def reconstruct_file = [ self . basename , self . extension ] . join
222
222
end
223
223
224
224
225
225
#
226
226
# ImmutablePath (Data)
227
227
#
228
228
229
- class ImmutablePath < Data . define ( :abs , :subdirs , :filename )
229
+ class ImmutablePath < Data . define ( :abs , :dirs , :file )
230
230
include PathMixin # also extends PathMixin::FactoryMethods
231
231
232
232
# rely on PathMixin#handle_init for initialize
@@ -236,35 +236,35 @@ def initialize(**kwargs)
236
236
237
237
# rely on Data#with for efficient copying
238
238
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 )
244
244
end
245
245
end
246
246
247
247
class MutablePath
248
248
include PathMixin # also extends PathMixin::FactoryMethods
249
249
250
- attr_reader :abs , :subdirs , :filename
250
+ attr_reader :abs , :dirs , :file
251
251
252
252
# rely on PathMixin#handle_init for initialize
253
253
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 )
256
256
end
257
257
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 )
261
261
end
262
262
263
263
# simple update
264
264
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
268
268
self
269
269
end
270
270
end
0 commit comments