Class: Helpers::Path
Overview
Path enables more intelligent handling of disk paths. Using Pathname as a foundation, Path instances can detect parent/child relationships, handle extension and name manipulations natively, and automate absolute/relative path distinctions.
Instance Attribute Summary collapse
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object
-
#absolute ⇒ Object
Return the absolute path of this Path object as a String.
- #absolute_path ⇒ Object
- #add_suffix(suffix) ⇒ Object
- #ascend(&block) ⇒ Object
-
#basename ⇒ String?
Returns the base of the filename.
-
#children ⇒ Array
Return the contained children of this Path as an array of Path objects.
-
#contains?(other) ⇒ TrueClass, FalseClass
Return whether this Path contains the given Path, Pathname, or String representing a file path.
- #descend(&block) ⇒ Object
-
#dirname ⇒ String
Returns the directory of this Path.
- #dup ⇒ Object
- #exists? ⇒ Boolean
- #extension ⇒ Object
- #file? ⇒ Boolean
-
#filename ⇒ String?
Returns the actual filename.
- #files ⇒ Object
- #folder? ⇒ Boolean
- #folders ⇒ Object
- #get_child_by_basename(basename) ⇒ Object
- #get_child_by_filename(filename) ⇒ Object
- #get_child_by_pathname(pathname) ⇒ Object
- #get_child_by_rootname(rootname) ⇒ Object
- #get_children ⇒ Object
-
#initialize(*args) ⇒ Path
constructor
A new instance of Path.
- #join(other) ⇒ Object
- #make_file! ⇒ Object
- #make_folder! ⇒ Object
- #markdown? ⇒ Boolean
-
#name ⇒ String
Returns the name of the path.
- #parent ⇒ Object
- #plaintext? ⇒ Boolean
- #pwd ⇒ Object
-
#relative ⇒ Object
Return the relative path of this Path object as a String.
- #relative_path ⇒ Object
-
#rootname ⇒ String
Return the name of the last element of the Path.
- #stringify ⇒ Object
- #to_s ⇒ Object
- #url_for(prefix = nil) ⇒ Object
-
#with_extension(new_extension) ⇒ Object
Return another Path but with the file’s extension changed to the one passed in.
Constructor Details
#initialize(*args) ⇒ Path
Returns a new instance of Path.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/helpers/path.rb', line 17 def initialize(*args) if args.length > 1 path = File.join(*args) else path = args[0] end if path.nil? @pathname = pwd elsif path.is_a?(Pathname) @pathname = path elsif path.is_a?(String) @pathname = Pathname.new(path) elsif path.is_a?(Path) @pathname = path.pathname else raise "Path#initialize didn't get a recognizable argument. Must be String, Pathname, or another Path." end raise "@pathname was nil" if @pathname.nil? if not @pathname.absolute? @pathname = @pathname..relative_path_from(pwd) end @force_is_file = false @force_is_folder = false end |
Instance Attribute Details
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
9 10 11 |
# File 'lib/helpers/path.rb', line 9 def pathname @pathname end |
Class Method Details
Instance Method Details
#+(other) ⇒ Object
84 85 86 |
# File 'lib/helpers/path.rb', line 84 def + (other) join(other) end |
#==(other) ⇒ Object
75 76 77 78 |
# File 'lib/helpers/path.rb', line 75 def == (other) return false if not other.is_a?(Path) @pathname. == other.pathname. end |
#absolute ⇒ Object
Return the absolute path of this Path object as a String.
228 229 230 |
# File 'lib/helpers/path.rb', line 228 def absolute absolute_path.to_s end |
#absolute_path ⇒ Object
232 233 234 |
# File 'lib/helpers/path.rb', line 232 def absolute_path @pathname. end |
#add_suffix(suffix) ⇒ Object
193 194 195 |
# File 'lib/helpers/path.rb', line 193 def add_suffix(suffix) file? ? Path.new(@pathname.parent + (basename + "_#{suffix}" + extension)) : nil end |
#ascend(&block) ⇒ Object
251 252 253 |
# File 'lib/helpers/path.rb', line 251 def ascend(&block) @pathname.descend(&block) end |
#basename ⇒ String?
Returns the base of the filename
/path/to/file.md -- file
/path/to/folder -- nil
144 145 146 |
# File 'lib/helpers/path.rb', line 144 def basename folder? ? nil : @pathname.basename(extension).to_s end |
#children ⇒ Array
Return the contained children of this Path as an array of Path objects.
270 271 272 |
# File 'lib/helpers/path.rb', line 270 def children @children ||= get_children end |
#contains?(other) ⇒ TrueClass, FalseClass
Return whether this Path contains the given Path, Pathname, or String representing a file path.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/helpers/path.rb', line 202 def contains?(other) if other.is_a?(String) path = Path.new(Pathname.new(other)) elsif other.is_a?(Pathname) path = Path.new(other) elsif other.is_a?(Path) path = other else raise "Pathname#contains got an argument it didn't recognize." end other.absolute.start_with?(absolute) end |
#descend(&block) ⇒ Object
247 248 249 |
# File 'lib/helpers/path.rb', line 247 def descend(&block) @pathname.descend(&block) end |
#dirname ⇒ String
Returns the directory of this Path
/path/to/file.md -- /path/to
/path/to/folder -- /path/to/folder
164 165 166 167 |
# File 'lib/helpers/path.rb', line 164 def dirname tr = folder? ? @pathname.to_s : @pathname.dirname.to_s tr == "." ? "" : tr end |
#exists? ⇒ Boolean
255 256 257 |
# File 'lib/helpers/path.rb', line 255 def exists? @pathname.exist? end |
#extension ⇒ Object
179 180 181 |
# File 'lib/helpers/path.rb', line 179 def extension @pathname.extname end |
#file? ⇒ Boolean
263 264 265 |
# File 'lib/helpers/path.rb', line 263 def file? @force_is_file ? true : @pathname.file? end |
#filename ⇒ String?
Returns the actual filename
/path/to/file.md -- file.md
/path/to/folder -- nil
154 155 156 |
# File 'lib/helpers/path.rb', line 154 def filename folder? ? nil : @pathname.basename.to_s end |
#files ⇒ Object
282 283 284 |
# File 'lib/helpers/path.rb', line 282 def files children.select {|path| path.file?} end |
#folder? ⇒ Boolean
259 260 261 |
# File 'lib/helpers/path.rb', line 259 def folder? @force_is_folder ? true : @pathname.directory? end |
#folders ⇒ Object
286 287 288 |
# File 'lib/helpers/path.rb', line 286 def folders children.select {|path| path.folder?} end |
#get_child_by_basename(basename) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/helpers/path.rb', line 112 def get_child_by_basename(basename) return nil if children.nil? children.each do |c| return c if c.basename == basename end nil end |
#get_child_by_filename(filename) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/helpers/path.rb', line 120 def get_child_by_filename(filename) return nil if children.nil? children.each do |c| return c if c.filename == filename end nil end |
#get_child_by_pathname(pathname) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/helpers/path.rb', line 96 def get_child_by_pathname(pathname) return nil if children.nil? children.each do |c| return c if c.pathname == pathname end nil end |
#get_child_by_rootname(rootname) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/helpers/path.rb', line 104 def get_child_by_rootname(rootname) return nil if children.nil? children.each do |c| return c if c.rootname == rootname end nil end |
#get_children ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/helpers/path.rb', line 274 def get_children # Run these checks first. If the pathname doesn't exist, we can't check for its children. If # the folder has no children, it's nil. return nil if not exists? return nil if @pathname.children.nil? folder? ? @pathname.children.collect {|c| Path.new(c)} : nil end |
#join(other) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/helpers/path.rb', line 88 def join(other) if other.is_a?(Path) Path.new(@pathname + other.pathname) else Path.new(@pathname + other) end end |
#make_file! ⇒ Object
53 54 55 56 |
# File 'lib/helpers/path.rb', line 53 def make_file! @force_is_file = true @force_is_folder = false end |
#make_folder! ⇒ Object
58 59 60 61 |
# File 'lib/helpers/path.rb', line 58 def make_folder! @force_is_file = false @force_is_folder = true end |
#markdown? ⇒ Boolean
67 68 69 |
# File 'lib/helpers/path.rb', line 67 def markdown? [".md", ".mdown", ".mkd", ".markdown", ".markd"].include?(extension) end |
#name ⇒ String
Returns the name of the path
/path/to/file.md -- file.md
/path/to/folder -- folder
134 135 136 |
# File 'lib/helpers/path.rb', line 134 def name folder? ? @pathname.basename.to_s : filename.to_s end |
#parent ⇒ Object
63 64 65 |
# File 'lib/helpers/path.rb', line 63 def parent Path.new(@pathname.parent) end |
#plaintext? ⇒ Boolean
71 72 73 |
# File 'lib/helpers/path.rb', line 71 def plaintext? [".txt", ".text"].include?(extension) end |
#pwd ⇒ Object
219 220 221 |
# File 'lib/helpers/path.rb', line 219 def pwd Pathname.pwd end |
#relative ⇒ Object
Return the relative path of this Path object as a String.
237 238 239 240 241 |
# File 'lib/helpers/path.rb', line 237 def relative tr = relative_path.to_s return "" if tr == "." tr end |
#relative_path ⇒ Object
243 244 245 |
# File 'lib/helpers/path.rb', line 243 def relative_path @pathname..relative_path_from(pwd.) end |
#rootname ⇒ String
Return the name of the last element of the Path
/path/to/file.md -- file.md
/path/to/folder -- folder
175 176 177 |
# File 'lib/helpers/path.rb', line 175 def rootname @pathname.basename.to_s end |
#stringify ⇒ Object
80 81 82 |
# File 'lib/helpers/path.rb', line 80 def stringify "Path(#{absolute})" end |
#to_s ⇒ Object
223 224 225 |
# File 'lib/helpers/path.rb', line 223 def to_s @pathname.to_s end |
#url_for(prefix = nil) ⇒ Object
290 291 292 |
# File 'lib/helpers/path.rb', line 290 def url_for(prefix=nil) Url.new(self, prefix=prefix) end |
#with_extension(new_extension) ⇒ Object
Return another Path but with the file’s extension changed to the one passed in.
184 185 186 187 188 189 190 191 |
# File 'lib/helpers/path.rb', line 184 def with_extension(new_extension) # This method should not return the same value, ever. if exists? file? ? Path.new(relative_path.dirname + (basename + new_extension)) : nil else Path.new(relative_path.dirname + (basename + new_extension)) end end |