Class: Helpers::Path

Inherits:
Object show all
Defined in:
lib/helpers/path.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.expand_path.relative_path_from(pwd)
  end
  @force_is_file = false
  @force_is_folder = false
end

Instance Attribute Details

#pathnameObject (readonly)

Returns the value of attribute pathname.



9
10
11
# File 'lib/helpers/path.rb', line 9

def pathname
  @pathname
end

Class Method Details

.join(path1, path2) ⇒ Object



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

def self.join(path1, path2)
  # TODO This is rather expensive. Make the join more efficient.
  Path.new(path1).join(Path.new(path2))
  #Pathname.new(path1).join(Pathname.new(path2)).to_s
end

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.expand_path == other.pathname.expand_path
end

#absoluteObject

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_pathObject



232
233
234
# File 'lib/helpers/path.rb', line 232

def absolute_path
  @pathname.expand_path
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

#basenameString?

Returns the base of the filename

/path/to/file.md -- file
/path/to/folder  -- nil

Returns:



144
145
146
# File 'lib/helpers/path.rb', line 144

def basename
  folder? ? nil : @pathname.basename(extension).to_s
end

#childrenArray

Return the contained children of this Path as an array of Path objects.

Returns:

  • (Array)

    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.

Parameters:

  • other (Path, Pathname, String)

    The file or folder to be checked.

Returns:

  • (TrueClass, FalseClass)

    Whether the given file or folder is contained in this 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

#dirnameString

Returns the directory of this Path

/path/to/file.md -- /path/to
/path/to/folder  -- /path/to/folder

Returns:



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

#dupObject



49
50
51
# File 'lib/helpers/path.rb', line 49

def dup
  Path.new(@pathname.dup)
end

#exists?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/helpers/path.rb', line 255

def exists?
  @pathname.exist?
end

#extensionObject



179
180
181
# File 'lib/helpers/path.rb', line 179

def extension
  @pathname.extname
end

#file?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/helpers/path.rb', line 263

def file?
  @force_is_file ? true : @pathname.file?
end

#filenameString?

Returns the actual filename

/path/to/file.md -- file.md
/path/to/folder  -- nil

Returns:



154
155
156
# File 'lib/helpers/path.rb', line 154

def filename
  folder? ? nil : @pathname.basename.to_s
end

#filesObject



282
283
284
# File 'lib/helpers/path.rb', line 282

def files
  children.select {|path| path.file?}
end

#folder?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/helpers/path.rb', line 259

def folder?
  @force_is_folder ? true : @pathname.directory?
end

#foldersObject



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_childrenObject



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

Returns:

  • (Boolean)


67
68
69
# File 'lib/helpers/path.rb', line 67

def markdown?
  [".md", ".mdown", ".mkd", ".markdown", ".markd"].include?(extension)
end

#nameString

Returns the name of the path

/path/to/file.md -- file.md
/path/to/folder  -- folder

Returns:



134
135
136
# File 'lib/helpers/path.rb', line 134

def name
  folder? ? @pathname.basename.to_s : filename.to_s
end

#parentObject



63
64
65
# File 'lib/helpers/path.rb', line 63

def parent
  Path.new(@pathname.parent)
end

#plaintext?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/helpers/path.rb', line 71

def plaintext?
  [".txt", ".text"].include?(extension)
end

#pwdObject



219
220
221
# File 'lib/helpers/path.rb', line 219

def pwd
  Pathname.pwd
end

#relativeObject

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_pathObject



243
244
245
# File 'lib/helpers/path.rb', line 243

def relative_path
  @pathname.expand_path.relative_path_from(pwd.expand_path)
end

#rootnameString

Return the name of the last element of the Path

/path/to/file.md -- file.md
/path/to/folder  -- folder

Returns:



175
176
177
# File 'lib/helpers/path.rb', line 175

def rootname
  @pathname.basename.to_s
end

#stringifyObject



80
81
82
# File 'lib/helpers/path.rb', line 80

def stringify
  "Path(#{absolute})"
end

#to_sObject



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