Class: Utopia::Path

Inherits:
Object
  • Object
show all
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.



12
13
14
15
# File 'lib/utopia/path.rb', line 12

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.



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

def components
  @components
end

Class Method Details

.[](path) ⇒ Object

Shorthand constructor



18
19
20
# File 'lib/utopia/path.rb', line 18

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

.create(path) ⇒ Object



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

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



199
200
201
202
203
204
205
206
207
# File 'lib/utopia/path.rb', line 199

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



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

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



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

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

#==(other) ⇒ Object



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

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

#absolute?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/utopia/path.rb', line 53

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

#ascend(&block) ⇒ Object



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

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



106
107
108
109
110
111
112
113
114
# File 'lib/utopia/path.rb', line 106

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)


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

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

#dirname(count = 1) ⇒ Object



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

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

	return absolute? ? path.to_absolute : path
end

#dupObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#extensionObject



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

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

#hashObject



187
188
189
# File 'lib/utopia/path.rb', line 187

def hash
	@components.hash
end

#join(other) ⇒ Object



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

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

#lastObject



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

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

#locale(extension = false) ⇒ Object



209
210
211
# File 'lib/utopia/path.rb', line 209

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

#simplifyObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/utopia/path.rb', line 91

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



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

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

#to_absoluteObject



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

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

#to_directoryObject



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

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

#to_local_pathObject



130
131
132
# File 'lib/utopia/path.rb', line 130

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

#to_sObject



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

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