Class: Pathname
Class Method Summary collapse
-
./(path) ⇒ Object
Start a path.
-
.[](path) ⇒ Object
Alternate to Pathname#new.
-
.home ⇒ Object
Home constant for building paths from root directory onward.
-
.null ⇒ Object
Platform dependent null device.
-
.root ⇒ Object
Root constant for building paths from root directory onward.
-
.work ⇒ Object
Work constant for building paths from root directory onward.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#glob(match, *opts) ⇒ Object
Glob pathnames.
-
#glob_first(match, *opts) ⇒ Object
Return the first glob match.
-
#glob_relative(match, *opts) ⇒ Object
Return globbed matches with pathnames relative to the current pathname.
-
#include?(pattern, *opts) ⇒ Boolean
Does a directory contain a matching entry? Or if the pathname is a file, same as #fnmatch.
- #outofdate?(*sources) ⇒ Boolean
- #rootname ⇒ Object
- #split_root ⇒ Object
- #uptodate?(*sources) ⇒ Boolean
-
#visit(options = {:all => false, :hidden => false}) ⇒ Object
Recursively visit a directory located by its path, yielding each resource as its full matching pathname object.
Class Method Details
./(path) ⇒ Object
Start a path. Another alias for #new.
Pathname / 'usr'
45 46 47 |
# File 'lib/standard/facets/pathname.rb', line 45 def self./(path) new(path) end |
.[](path) ⇒ Object
Alternate to Pathname#new.
Pathname['/usr/share']
37 38 39 |
# File 'lib/standard/facets/pathname.rb', line 37 def self.[](path) new(path) end |
.home ⇒ Object
Home constant for building paths from root directory onward.
TODO: Pathname#home needs to be more robust.
58 59 60 |
# File 'lib/standard/facets/pathname.rb', line 58 def self.home Pathname.new('~') end |
.null ⇒ Object
Platform dependent null device.
CREDIT Daniel Burger
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/standard/facets/pathname.rb', line 71 def self.null case RUBY_PLATFORM when /mswin/i 'NUL' when /amiga/i 'NIL:' when /openvms/i 'NL:' else '/dev/null' end end |
Instance Method Details
#empty? ⇒ Boolean
140 141 142 |
# File 'lib/standard/facets/pathname.rb', line 140 def empty? Dir.glob(::File.join(to_s, '*')).empty? end |
#glob(match, *opts) ⇒ Object
Glob pathnames.
99 100 101 102 |
# File 'lib/standard/facets/pathname.rb', line 99 def glob(match, *opts) flags = glob_flags(opts) Dir.glob(::File.join(self.to_s, match), flags).collect{ |m| self.class.new(m) } end |
#glob_first(match, *opts) ⇒ Object
Return the first glob match.
DEPRECATE: While slightly faster then glob().first, not really worth it unless this can be rewritten to shortcut on first match (using fnmatch?). In wich case, is there a better name for this method?
109 110 111 112 113 |
# File 'lib/standard/facets/pathname.rb', line 109 def glob_first(match, *opts) flags = glob_flags(opts) file = ::Dir.glob(::File.join(self.to_s, match), flags).first file ? self.class.new(file) : nil end |
#glob_relative(match, *opts) ⇒ Object
Return globbed matches with pathnames relative to the current pathname.
116 117 118 119 120 121 |
# File 'lib/standard/facets/pathname.rb', line 116 def glob_relative(match, *opts) flags = glob_flags(opts) files = Dir.glob(::File.join(self.to_s, match), flags) files = files.map{ |f| f.sub(self.to_s.chomp('/') + '/', '') } files.collect{ |m| self.class.new(m) } end |
#include?(pattern, *opts) ⇒ Boolean
Does a directory contain a matching entry? Or if the pathname is a file, same as #fnmatch.
222 223 224 225 226 227 228 |
# File 'lib/standard/facets/pathname.rb', line 222 def include?(pattern,*opts) if directory? glob_first(pattern,*opts) else fnmatch(pattern,*opts) end end |
#outofdate?(*sources) ⇒ Boolean
150 151 152 |
# File 'lib/standard/facets/pathname.rb', line 150 def outofdate?(*sources) ::FileUtils.outofdate?(to_s, sources.flatten) end |
#rootname ⇒ Object
88 89 90 |
# File 'lib/standard/facets/pathname.rb', line 88 def rootname self.class.new(File.rootname(to_s)) end |
#split_root ⇒ Object
93 94 95 96 |
# File 'lib/standard/facets/pathname.rb', line 93 def split_root head, tail = *::File.split_root(to_s) [self.class.new(head), self.class.new(tail)] end |
#uptodate?(*sources) ⇒ Boolean
145 146 147 |
# File 'lib/standard/facets/pathname.rb', line 145 def uptodate?(*sources) ::FileUtils.uptodate?(to_s, sources.flatten) end |
#visit(options = {:all => false, :hidden => false}) ⇒ Object
Recursively visit a directory located by its path, yielding each resource as its full matching pathname object. If called on a file, yield the file.
call-seq:
visit => yield each file
visit(all: true) => yield visited directories as well
visit(hidden: true) => yield hidden files and directories as well
Example use case:
# Locate any file but *.haml within app/**/*
Pathname.new("app").visit do |f|
next unless f.to_s =~ /\.haml$/
f
end
TODO: Use #map instead of #each ?
CREDIT: Jean-Denis Vauguet
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/standard/facets/pathname.rb', line 173 def visit( = {:all => false, :hidden => false}) if self.directory? children.each do |entry| next if entry.basename.to_s[0] == "." && ![:hidden] yield(entry) unless entry.directory? && ![:all] ##entry.visit(:all => options[:all]) { |sub_entry| yield sub_entry } if entry.directory? entry.visit(:all => [:all], :hidden => [:hidden]) do |sub_entry| yield(sub_entry) end if entry.directory? end else yield self end end |