Class: Dotum::Util::Path
- Inherits:
-
Object
- Object
- Dotum::Util::Path
- 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
- #==(other) ⇒ Object
- #basename ⇒ Object
- #directory? ⇒ Boolean
- #dirname ⇒ Object
- #exists? ⇒ Boolean
- #file? ⇒ Boolean
- #glob(expression, &filter) ⇒ Object
- #hidden? ⇒ Boolean
-
#initialize(path, relative = nil) ⇒ Path
constructor
A new instance of Path.
- #join(*components) ⇒ Object
- #link_path ⇒ Object
- #mkpath! ⇒ Object
- #pretty ⇒ Object
- #read ⇒ Object
- #relative_glob(expression, &filter) ⇒ Object
- #symlink? ⇒ Boolean
- #to_s ⇒ Object
- #to_str ⇒ Object
- #write(content) ⇒ Object
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.(path, relative) end |
Class Method Details
.home_dir ⇒ Object
59 60 61 |
# File 'lib/dotum/util/path.rb', line 59 def self.home_dir @home_dir ||= File.("~") 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 |
#basename ⇒ Object
86 87 88 |
# File 'lib/dotum/util/path.rb', line 86 def basename File.basename(@path) end |
#directory? ⇒ Boolean
35 36 37 |
# File 'lib/dotum/util/path.rb', line 35 def directory? File.directory? @path end |
#dirname ⇒ Object
82 83 84 |
# File 'lib/dotum/util/path.rb', line 82 def dirname self.class.new(File.dirname(@path)) end |
#exists? ⇒ Boolean
27 28 29 |
# File 'lib/dotum/util/path.rb', line 27 def exists? File.exist? @path end |
#file? ⇒ 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
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 |
#link_path ⇒ Object
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 |
#pretty ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/dotum/util/path.rb', line 63 def pretty home_dir = File.("~") if @path.start_with? home_dir return "~" + @path[home_dir.size..-1] end @path end |
#read ⇒ Object
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
39 40 41 |
# File 'lib/dotum/util/path.rb', line 39 def symlink? File.symlink? @path end |
#to_s ⇒ Object
51 52 53 |
# File 'lib/dotum/util/path.rb', line 51 def to_s @path end |
#to_str ⇒ Object
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 |