Class: Munge::Util::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/munge/util/path.rb

Class Method Summary collapse

Class Method Details

.basename(path) ⇒ Object



30
31
32
# File 'lib/munge/util/path.rb', line 30

def self.basename(path)
  File.basename(path)
end

.basename_no_extension(path) ⇒ Object



34
35
36
37
38
39
# File 'lib/munge/util/path.rb', line 34

def self.basename_no_extension(path)
  filename = basename(path)
  filename_parts = filename.split(".")

  filename_parts[0] || ""
end

.dirname(path) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/munge/util/path.rb', line 4

def self.dirname(path)
  if File.dirname(path) == "."
    ""
  else
    File.dirname(path)
  end
end

.ensure_abspath(path) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/munge/util/path.rb', line 51

def self.ensure_abspath(path)
  correct = path.squeeze("/")

  if correct[0] == "/"
    correct
  else
    "/#{correct}"
  end
end

.ensure_relpath(path) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/munge/util/path.rb', line 61

def self.ensure_relpath(path)
  correct = path.squeeze("/")

  if correct[0] == "/"
    correct[1..-1]
  else
    correct
  end
end

.extname(path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/munge/util/path.rb', line 12

def self.extname(path)
  filename = basename(path)
  filename_parts = filename.split(".")

  if filename_parts.length > 1
    filename_parts[-1]
  else
    ""
  end
end

.extnames(path) ⇒ Object



23
24
25
26
27
28
# File 'lib/munge/util/path.rb', line 23

def self.extnames(path)
  filename = basename(path)
  filename_parts = filename.split(".")

  filename_parts[1..-1]
end

.join(*path_components) ⇒ Object



71
72
73
74
75
# File 'lib/munge/util/path.rb', line 71

def self.join(*path_components)
  path_components
    .reject(&:empty?)
    .join("/")
end

.path_no_extension(path) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/munge/util/path.rb', line 41

def self.path_no_extension(path)
  extension = extname(path)

  if extension == ""
    path
  else
    path.sub(/\.#{extension}/, "")
  end
end