Class: Utopia::Path
Constant Summary collapse
- SEPARATOR =
"/"
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
Class Method Summary collapse
-
.[](path) ⇒ Object
Shorthand constructor.
- .create(path) ⇒ Object
- .locale(name, extension = false) ⇒ Object
- .unescape(string) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
-
#-(other) ⇒ Object
Computes the difference of the path.
- #<=>(other) ⇒ Object
- #[](index) ⇒ Object
- #absolute? ⇒ Boolean
- #ascend(&block) ⇒ Object
- #basename(ext = nil) ⇒ Object
- #descend(&block) ⇒ Object
- #directory? ⇒ Boolean
- #dirname(count = 1) ⇒ Object
- #dup ⇒ Object
- #eql?(other) ⇒ Boolean
- #extension ⇒ Object
- #hash ⇒ Object
-
#initialize(components = []) ⇒ Path
constructor
A new instance of Path.
- #join(other) ⇒ Object
- #last ⇒ Object
- #locale(extension = false) ⇒ Object
- #simplify ⇒ Object
- #split(at) ⇒ Object
- #starts_with?(other) ⇒ Boolean
- #to_absolute ⇒ Object
- #to_directory ⇒ Object
- #to_local_path ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(components = []) ⇒ Path
Returns a new instance of Path.
27 28 29 30 |
# File 'lib/utopia/path.rb', line 27 def initialize(components = []) # To ensure we don't do anything stupid we freeze the components @components = components.dup.freeze end |
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
62 63 64 |
# File 'lib/utopia/path.rb', line 62 def components @components end |
Class Method Details
.[](path) ⇒ Object
Shorthand constructor
33 34 35 |
# File 'lib/utopia/path.rb', line 33 def self.[](path) create(path) end |
.create(path) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/utopia/path.rb', line 43 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 when Symbol return Path.new([path]) end end |
.locale(name, extension = false) ⇒ Object
254 255 256 257 258 259 260 261 262 |
# File 'lib/utopia/path.rb', line 254 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
37 38 39 40 41 |
# File 'lib/utopia/path.rb', line 37 def self.unescape(string) string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) { [$1.delete('%')].pack('H*') } end |
Instance Method Details
#+(other) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/utopia/path.rb', line 108 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
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/utopia/path.rb', line 125 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
222 223 224 |
# File 'lib/utopia/path.rb', line 222 def <=> other @components <=> other.components end |
#[](index) ⇒ Object
100 101 102 |
# File 'lib/utopia/path.rb', line 100 def [] index @components[index] end |
#absolute? ⇒ Boolean
76 77 78 |
# File 'lib/utopia/path.rb', line 76 def absolute? return @components.first == "" end |
#ascend(&block) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/utopia/path.rb', line 192 def ascend(&block) return to_enum(:ascend) unless block_given? next_parent = self begin parent = next_parent yield parent next_parent = parent.dirname end until next_parent.eql?(parent) end |
#basename(ext = nil) ⇒ Object
152 153 154 155 156 157 158 159 160 |
# File 'lib/utopia/path.rb', line 152 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 |
#descend(&block) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/utopia/path.rb', line 180 def descend(&block) return to_enum(:descend) unless block_given? parent_path = [] components.each do |component| parent_path << component yield self.class.new(parent_path) end end |
#directory? ⇒ Boolean
64 65 66 |
# File 'lib/utopia/path.rb', line 64 def directory? return @components.last == "" end |
#dirname(count = 1) ⇒ Object
170 171 172 173 174 |
# File 'lib/utopia/path.rb', line 170 def dirname(count = 1) path = Path.new(components[0...-count]) return absolute? ? path.to_absolute : path end |
#dup ⇒ Object
218 219 220 |
# File 'lib/utopia/path.rb', line 218 def dup return Path.new(components.dup) end |
#eql?(other) ⇒ Boolean
226 227 228 229 230 231 232 |
# File 'lib/utopia/path.rb', line 226 def eql? other if self.class == other.class return @components.eql?(other.components) else return false end end |
#extension ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/utopia/path.rb', line 162 def extension if components.last components.last.split(".").last else nil end end |
#hash ⇒ Object
242 243 244 |
# File 'lib/utopia/path.rb', line 242 def hash @components.hash end |
#join(other) ⇒ Object
104 105 106 |
# File 'lib/utopia/path.rb', line 104 def join(other) Path.new(@components + other).simplify end |
#last ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/utopia/path.rb', line 246 def last if directory? components[-2] else components[-1] end end |
#locale(extension = false) ⇒ Object
264 265 266 |
# File 'lib/utopia/path.rb', line 264 def locale (extension = false) return Path.locale(last, extension) end |
#simplify ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/utopia/path.rb', line 137 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
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/utopia/path.rb', line 206 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
234 235 236 237 238 239 240 |
# File 'lib/utopia/path.rb', line 234 def starts_with? other other.components.each_with_index do |part, index| return false if @components[index] != part end return true end |
#to_absolute ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/utopia/path.rb', line 80 def to_absolute if absolute? return self else return Path.new([""] + @components) end end |
#to_directory ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/utopia/path.rb', line 68 def to_directory if directory? return self else return join([""]) end end |
#to_local_path ⇒ Object
176 177 178 |
# File 'lib/utopia/path.rb', line 176 def to_local_path components.join(File::SEPARATOR) end |
#to_s ⇒ Object
96 97 98 |
# File 'lib/utopia/path.rb', line 96 def to_s to_str end |