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.



14
15
16
17
# File 'lib/utopia/path.rb', line 14

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.



41
42
43
# File 'lib/utopia/path.rb', line 41

def components
  @components
end

Class Method Details

.[](path) ⇒ Object

Shorthand constructor



20
21
22
# File 'lib/utopia/path.rb', line 20

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

.create(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/utopia/path.rb', line 24

def self.create(path)
	case path
	when Path
		return path
	when Array
		return Path.new(path)
	when String
		path = Rack::Utils.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



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

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

Instance Method Details

#+(other) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/utopia/path.rb', line 79

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



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

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



184
185
186
# File 'lib/utopia/path.rb', line 184

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

#absolute?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/utopia/path.rb', line 55

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

#ascend(&block) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/utopia/path.rb', line 151

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



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

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)


43
44
45
# File 'lib/utopia/path.rb', line 43

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

#dirname(count = 1) ⇒ Object



141
142
143
144
145
# File 'lib/utopia/path.rb', line 141

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

	return absolute? ? path.to_absolute : path
end

#dupObject



180
181
182
# File 'lib/utopia/path.rb', line 180

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#extensionObject



133
134
135
136
137
138
139
# File 'lib/utopia/path.rb', line 133

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

#hashObject



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

def hash
	@components.hash
end

#join(other) ⇒ Object



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

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

#lastObject



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

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

#locale(extension = false) ⇒ Object



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

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

#simplifyObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/utopia/path.rb', line 108

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



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/utopia/path.rb', line 168

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)


196
197
198
199
200
201
202
# File 'lib/utopia/path.rb', line 196

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

#to_absoluteObject



59
60
61
62
63
64
65
# File 'lib/utopia/path.rb', line 59

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

#to_directoryObject



47
48
49
50
51
52
53
# File 'lib/utopia/path.rb', line 47

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

#to_local_pathObject



147
148
149
# File 'lib/utopia/path.rb', line 147

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

#to_sObject



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

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