Class: Utopia::Path

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/utopia/path.rb,
lib/utopia/path/matcher.rb

Defined Under Namespace

Classes: Matcher

Constant Summary collapse

SEPARATOR =
'/'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components = []) ⇒ Path

Returns a new instance of Path.



73
74
75
# File 'lib/utopia/path.rb', line 73

def initialize(components = [])
	@components = components
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



77
78
79
# File 'lib/utopia/path.rb', line 77

def components
  @components
end

Class Method Details

.[](path) ⇒ Object



119
120
121
# File 'lib/utopia/path.rb', line 119

def self.[] path
	self.create(path)
end

.create(path) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/utopia/path.rb', line 136

def self.create(path)
	case path
	when Path
		return path
	when Array
		return self.new(path)
	when String
		return self.new(unescape(path).split(SEPARATOR, -1))
	else
		return self.new([path])
	end
end

.prefix_length(a, b) ⇒ Object

Returns the length of the prefix which is shared by two strings.



90
91
92
# File 'lib/utopia/path.rb', line 90

def self.prefix_length(a, b)
	[a.size, b.size].min.times{|i| return i if a[i] != b[i]}
end

.rootObject



85
86
87
# File 'lib/utopia/path.rb', line 85

def self.root
	self.new([''])
end

.shortest_path(path, root) ⇒ Object

Return the shortest relative path to get to path from root:



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/utopia/path.rb', line 95

def self.shortest_path(path, root)
	path = self.create(path)
	root = self.create(root).dirname
	
	# Find the common prefix:
	i = prefix_length(path.components, root.components) || 0
	
	# The difference between the root path and the required path, taking into account the common prefix:
	up = root.components.size - i
	
	return self.create([".."] * up + path.components[i..-1])
end

.split(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/utopia/path.rb', line 123

def self.split(path)
	case path
	when Path
		return path.to_a
	when Array
		return path
	when String
		create(path).to_a
	else
		[path]
	end
end

.unescape(string) ⇒ Object

Converts ‘+’ into whitespace and hex encoded characters into their equivalent characters.



113
114
115
116
117
# File 'lib/utopia/path.rb', line 113

def self.unescape(string)
	string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) {
		[$1.delete('%')].pack('H*')
	}
end

Instance Method Details

#+(other) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/utopia/path.rb', line 205

def +(other)
	if other.kind_of? Path
		if other.absolute?
			return other
		else
			return join(other.components)
		end
	elsif other.kind_of? Array
		return join(other)
	elsif other.kind_of? String
		return join(other.split(SEPARATOR, -1))
	else
		return join([other.to_s])
	end
end

#-(other) ⇒ Object

Computes the difference of the path. /a/b/c - /a/b -> c a/b/c - a/b -> c



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/utopia/path.rb', line 228

def -(other)
	i = 0
	
	while i < other.components.size
		break if @components[i] != other.components[i]
		
		i += 1
	end
	
	return self.class.new(@components[i,@components.size])
end

#<=>(other) ⇒ Object



310
311
312
# File 'lib/utopia/path.rb', line 310

def <=> other
	@components <=> other.components
end

#==(other) ⇒ Object



322
323
324
325
326
327
328
329
330
# File 'lib/utopia/path.rb', line 322

def == other
	return false unless other
	
	case other
	when String then self.to_s == other
	when Array then self.to_a == other
	else other.is_a?(self.class) && @components == other.components
	end
end

#[](index) ⇒ Object



340
341
342
# File 'lib/utopia/path.rb', line 340

def [] index
	return @components[component_offset(index)]
end

#[]=(index, value) ⇒ Object

Replaces a named component, indexing as per



345
346
347
# File 'lib/utopia/path.rb', line 345

def []= index, value
	return @components[component_offset(index)] = value
end

#absolute?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/utopia/path.rb', line 169

def absolute?
	@components.first == ''
end

#ascend(&block) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
# File 'lib/utopia/path.rb', line 282

def ascend(&block)
	return to_enum(:ascend) unless block_given?
	
	components = self.components.dup
	
	while components.any?
		yield self.class.new(components.dup)
		
		components.pop
	end
end

#basename(*args) ⇒ Object



256
257
258
# File 'lib/utopia/path.rb', line 256

def basename(*args)
	Basename.new(@components.last, *args)
end

#delete_at(index) ⇒ Object



349
350
351
# File 'lib/utopia/path.rb', line 349

def delete_at(index)
	@components.delete_at(component_offset(index))
end

#descend(&block) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
# File 'lib/utopia/path.rb', line 270

def descend(&block)
	return to_enum(:descend) unless block_given?
	
	components = []
	
	@components.each do |component|
		components << component
		
		yield self.class.new(components.dup)
	end
end

#directory?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/utopia/path.rb', line 153

def directory?
	return @components.last == ''
end

#dirname(count = 1) ⇒ Object



260
261
262
263
264
# File 'lib/utopia/path.rb', line 260

def dirname(count = 1)
	path = self.class.new(@components[0...-count])

	return absolute? ? path.to_absolute : path
end

#dupObject



306
307
308
# File 'lib/utopia/path.rb', line 306

def dup
	return Path.new(components.dup)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/utopia/path.rb', line 314

def eql? other
	self.class.eql?(other.class) and @components.eql?(other.components)
end

#expand(root) ⇒ Object



201
202
203
# File 'lib/utopia/path.rb', line 201

def expand(root)
	root + self
end

#extensionObject



369
370
371
# File 'lib/utopia/path.rb', line 369

def extension
	basename(true).extension
end

#firstObject



353
354
355
356
357
358
359
# File 'lib/utopia/path.rb', line 353

def first
	if absolute?
		@components[1]
	else
		@components[0]
	end
end

#freezeObject



79
80
81
82
83
# File 'lib/utopia/path.rb', line 79

def freeze
	@components.freeze
	
	super
end

#hashObject



318
319
320
# File 'lib/utopia/path.rb', line 318

def hash
	@components.hash
end

#include?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(*args)
	@components.include?(*args)
end

#join(other) ⇒ Object



197
198
199
# File 'lib/utopia/path.rb', line 197

def join(other)
	self.class.new(@components + other).simplify
end

#lastObject



361
362
363
364
365
366
367
# File 'lib/utopia/path.rb', line 361

def last
	if directory?
		@components[-2]
	else
		@components[-1]
	end
end

#relative?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/utopia/path.rb', line 165

def relative?
	@components.first != ''
end

#shortest_path(root) ⇒ Object



108
109
110
# File 'lib/utopia/path.rb', line 108

def shortest_path(root)
	self.class.shortest_path(self, root)
end

#simplifyObject



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/utopia/path.rb', line 240

def simplify
	result = absolute? ? [''] : []

	@components.each do |bit|
		if bit == ".."
			result.pop
		elsif bit != "." && bit != ''
			result << bit
		end
	end

	result << '' if directory?
	
	return self.class.new(result)
end

#split(at) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/utopia/path.rb', line 294

def split(at)
	if at.kind_of? String
		at = @components.index(at)
	end
	
	if at
		return [self.class.new(@components[0...at]), self.class.new(@components[at+1..-1])]
	else
		return nil
	end
end

#start_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


332
333
334
335
336
337
338
# File 'lib/utopia/path.rb', line 332

def start_with? other
	other.components.each_with_index do |part, index|
		return false if @components[index] != part
	end
	
	return true
end

#to_aObject



193
194
195
# File 'lib/utopia/path.rb', line 193

def to_a
	@components
end

#to_absoluteObject



173
174
175
176
177
178
179
# File 'lib/utopia/path.rb', line 173

def to_absolute
	if absolute?
		return self
	else
		return self.class.new([''] + @components)
	end
end

#to_directoryObject



157
158
159
160
161
162
163
# File 'lib/utopia/path.rb', line 157

def to_directory
	if directory?
		return self
	else
		return join([''])
	end
end

#to_local_path(separator = File::SEPARATOR) ⇒ Object



266
267
268
# File 'lib/utopia/path.rb', line 266

def to_local_path(separator = File::SEPARATOR)
	@components.join(separator)
end

#to_sObject



189
190
191
# File 'lib/utopia/path.rb', line 189

def to_s
	to_str
end

#to_strObject



181
182
183
184
185
186
187
# File 'lib/utopia/path.rb', line 181

def to_str
	if @components == ['']
		SEPARATOR
	else
		@components.join(SEPARATOR)
	end
end

#with_prefix(*args) ⇒ Object



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

def with_prefix(*args)
	self.class.create(*args) + self
end