Class: Utopia::Path

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

Constant Summary collapse

SEPARATOR =
"/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components = []) ⇒ Path

Returns a new instance of Path.



28
29
30
31
# File 'lib/utopia/path.rb', line 28

def initialize(components = [])
	# To ensure we don't do anything stupid we freeze the components
	@components = components.dup.freeze
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



61
62
63
# File 'lib/utopia/path.rb', line 61

def components
  @components
end

Class Method Details

.[](path) ⇒ Object

Shorthand constructor



34
35
36
# File 'lib/utopia/path.rb', line 34

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

.create(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/utopia/path.rb', line 44

def self.create(path)
	case path
	when Path
		return path
	when Array
		return Path.new(path)
	when String
		path = unescape(path)
		# Ends with SEPARATOR
		if path[-1,1] == SEPARATOR
			return Path.new(path.split(SEPARATOR) << "")
		else
			return Path.new(path.split(SEPARATOR))
		end
	end
end

.locale(name, extension = false) ⇒ Object



236
237
238
239
240
241
242
243
244
# File 'lib/utopia/path.rb', line 236

def self.locale(name, extension = false)
	if String === extension
		name = File.basename(name, extension)
	elsif extension
		name = name.split
	end
	
	name.split(".")[1..-1].join(".")
end

.unescape(string) ⇒ Object



38
39
40
41
42
# File 'lib/utopia/path.rb', line 38

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

Instance Method Details

#+(other) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/utopia/path.rb', line 99

def +(other)
	if other.kind_of? Array
		return join(other)
	elsif other.kind_of? Path
		if other.absolute?
			return other
		else
			return join(other.components)
		end
	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



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/utopia/path.rb', line 116

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

#<=>(other) ⇒ Object



204
205
206
# File 'lib/utopia/path.rb', line 204

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

#absolute?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/utopia/path.rb', line 75

def absolute?
	return @components.first == ""
end

#ascend(&block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/utopia/path.rb', line 171

def ascend(&block)
	paths = []
	
	next_parent = self

	begin
		parent = next_parent
		
		yield parent if block_given?
		paths << parent
		
		next_parent = parent.dirname
	end until next_parent.eql?(parent)
	
	return paths
end

#basename(ext = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
# File 'lib/utopia/path.rb', line 143

def basename(ext = nil)
	if ext == true
		File.basename(components.last, extension)
	elsif String === ext
		File.basename(components.last, ext)
	else
		components.last
	end
end

#directory?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/utopia/path.rb', line 63

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

#dirname(count = 1) ⇒ Object



161
162
163
164
165
# File 'lib/utopia/path.rb', line 161

def dirname(count = 1)
	path = Path.new(components[0...-count])

	return absolute? ? path.to_absolute : path
end

#dupObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
211
212
213
214
# File 'lib/utopia/path.rb', line 208

def eql? other
	if self.class == other.class
		return @components.eql?(other.components)
	else
		return false
	end
end

#extensionObject



153
154
155
156
157
158
159
# File 'lib/utopia/path.rb', line 153

def extension
	if components.last
		components.last.split(".").last
	else
		nil
	end
end

#hashObject



224
225
226
# File 'lib/utopia/path.rb', line 224

def hash
	@components.hash
end

#join(other) ⇒ Object



95
96
97
# File 'lib/utopia/path.rb', line 95

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

#lastObject



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

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

#locale(extension = false) ⇒ Object



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

def locale (extension = false)
	return Path.locale(last, extension)
end

#simplifyObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/utopia/path.rb', line 128

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

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

	result << "" if directory?
	return Path.new(result)
end

#split(at) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/utopia/path.rb', line 188

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

#starts_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
# File 'lib/utopia/path.rb', line 216

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

#to_absoluteObject



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

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

#to_directoryObject



67
68
69
70
71
72
73
# File 'lib/utopia/path.rb', line 67

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

#to_local_pathObject



167
168
169
# File 'lib/utopia/path.rb', line 167

def to_local_path
	components.join(File::SEPARATOR)
end

#to_sObject



87
88
89
90
91
92
93
# File 'lib/utopia/path.rb', line 87

def to_s
	if @components == [""]
		SEPARATOR
	else
		@components.join(SEPARATOR)
	end
end