Class: Dotum::Util::Path

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

Overview

Very similar to ‘Pathname`.

Constant Summary collapse

ABSOLUTE_PATH_MATCHER =
/^(~?\/|\w+\:\\)/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, relative = nil) ⇒ Path

Returns a new instance of Path.



10
11
12
13
14
# File 'lib/dotum/util/path.rb', line 10

def initialize(path, relative=nil)
  path = path.to_str.gsub(/[\/\\]/, File::Separator)

  @path = File.expand_path(path, relative)
end

Class Method Details

.home_dirObject



59
60
61
# File 'lib/dotum/util/path.rb', line 59

def self.home_dir
  @home_dir ||= File.expand_path("~")
end

Instance Method Details

#==(other) ⇒ Object



112
113
114
# File 'lib/dotum/util/path.rb', line 112

def ==(other)
  to_str == other.to_str
end

#basenameObject



86
87
88
# File 'lib/dotum/util/path.rb', line 86

def basename
  File.basename(@path)
end

#directory?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dotum/util/path.rb', line 35

def directory?
  File.directory? @path
end

#dirnameObject



82
83
84
# File 'lib/dotum/util/path.rb', line 82

def dirname
  self.class.new(File.dirname(@path))
end

#exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/dotum/util/path.rb', line 27

def exists?
  File.exist? @path
end

#file?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dotum/util/path.rb', line 31

def file?
  File.file? @path
end

#glob(expression, &filter) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dotum/util/path.rb', line 90

def glob(expression, &filter)
  matches = Dir.glob(join(expression), File::FNM_DOTMATCH)

  if filter
    matches.reject! { |p| !filter.call(self.class.new(p)) }
  end

  matches.reject! { |path|
    basename = File.basename(path)

    basename == "." || basename == ".."
  }

  matches.map { |p| self.class.new(p) }
end

#hidden?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dotum/util/path.rb', line 43

def hidden?
  File.split(@path).any? { |p| p.start_with? "." }
end

#join(*components) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/dotum/util/path.rb', line 16

def join(*components)
  (components.size - 1).downto(0) do |i|
    # Do we have absolute paths?  Stop at the last one.
    if components[i].to_str =~ ABSOLUTE_PATH_MATCHER
      return self.class.new(File.join(*components[i..-1]))
    end
  end

  self.class.new(File.join(@path, *components))
end


47
48
49
# File 'lib/dotum/util/path.rb', line 47

def link_path
  self.class.new(File.readlink(@path))
end

#mkpath!Object



116
117
118
# File 'lib/dotum/util/path.rb', line 116

def mkpath!
  FileUtils.mkpath(@path)
end

#prettyObject



63
64
65
66
67
68
69
70
# File 'lib/dotum/util/path.rb', line 63

def pretty
  home_dir = File.expand_path("~")
  if @path.start_with? home_dir
    return "~" + @path[home_dir.size..-1]
  end

  @path
end

#readObject



72
73
74
# File 'lib/dotum/util/path.rb', line 72

def read
  File.read(@path)
end

#relative_glob(expression, &filter) ⇒ Object



106
107
108
109
110
# File 'lib/dotum/util/path.rb', line 106

def relative_glob(expression, &filter)
  glob(expression, &filter).map { |path|
    path.to_str[(@path.size + 1)..-1] || "."
  }
end

#symlink?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dotum/util/path.rb', line 39

def symlink?
  File.symlink? @path
end

#to_sObject



51
52
53
# File 'lib/dotum/util/path.rb', line 51

def to_s
  @path
end

#to_strObject



55
56
57
# File 'lib/dotum/util/path.rb', line 55

def to_str
  @path
end

#write(content) ⇒ Object



76
77
78
79
80
# File 'lib/dotum/util/path.rb', line 76

def write(content)
  File.open(@path, "w") do |file|
    file.write(content)
  end
end