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.



23
24
25
26
# File 'lib/utopia/path.rb', line 23

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.



50
51
52
# File 'lib/utopia/path.rb', line 50

def components
  @components
end

Class Method Details

.[](path) ⇒ Object

Shorthand constructor



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

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

.create(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/utopia/path.rb', line 33

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

Instance Method Details

#+(other) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/utopia/path.rb', line 88

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



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

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

#==(other) ⇒ Object



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

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

#absolute?Boolean

Returns:

  • (Boolean)


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

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

#ascend(&block) ⇒ Object



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

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



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

def basename(ext = nil)
	if ext
		File.basename(components.last, ext)
	else
		components.last
	end
end

#directory?Boolean

Returns:

  • (Boolean)


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

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

#dirname(count = 1) ⇒ Object



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

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

	return absolute? ? path.to_absolute : path
end

#dupObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



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

def hash
	@components.hash
end

#join(other) ⇒ Object



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

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

#simplifyObject



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

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



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

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



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

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

#to_directoryObject



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

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

#to_local_pathObject



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

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

#to_sObject



76
77
78
79
80
81
82
# File 'lib/utopia/path.rb', line 76

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