Class: Utopia::Path
- Inherits:
-
Object
- Object
- Utopia::Path
- Defined in:
- lib/utopia/path.rb
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
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #absolute? ⇒ Boolean
- #ascend(&block) ⇒ Object
- #basename(ext = nil) ⇒ Object
- #directory? ⇒ Boolean
- #dirname(count = 1) ⇒ Object
- #dup ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(components) ⇒ Path
constructor
A new instance of Path.
- #join(other) ⇒ Object
- #simplify ⇒ Object
- #split(at) ⇒ Object
- #to_absolute ⇒ Object
- #to_directory ⇒ Object
- #to_local_path ⇒ Object
- #to_s ⇒ Object
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
#components ⇒ Object (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
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
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 |
#dup ⇒ Object
164 165 166 |
# File 'lib/utopia/path.rb', line 164 def dup return Path.new(components.dup) end |
#eql?(other) ⇒ 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 |
#hash ⇒ Object
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 |
#simplify ⇒ Object
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_absolute ⇒ Object
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_directory ⇒ Object
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_path ⇒ Object
131 132 133 |
# File 'lib/utopia/path.rb', line 131 def to_local_path components.join(File::SEPARATOR) end |