class Pathname
Public Class Methods
Start a path. Another alias for new.
Pathname / 'usr'
# File lib/standard/facets/pathname/op_div.rb, line 7 def self./(path) new(path) end
Alternate to Pathname#new.
Pathname['/usr/share']
Returns [Pathname]
# File lib/standard/facets/pathname/op_fetch.rb, line 9 def self.[](path) new(path) end
Home constant for building paths from root directory onward.
TODO: Pathname#home needs to be more robust.
Returns [Pathname]
# File lib/standard/facets/pathname/home.rb, line 8 def self.home Pathname.new('~') end
Platform dependent null device.
CREDIT: Daniel Burger
# File lib/standard/facets/pathname/null.rb, line 7 def self.null case RUBY_PLATFORM when /mswin/i 'NUL' when /amiga/i 'NIL:' when /openvms/i 'NL:' else '/dev/null' end end
Root constant for building paths from root directory onward.
Returns [Pathname]
# File lib/standard/facets/pathname/root.rb, line 6 def self.root Pathname.new('/') end
Work constant for building paths from root directory onward.
Returns [Pathname]
# File lib/standard/facets/pathname/work.rb, line 6 def self.work Pathname.new('.') end
Public Instance Methods
Change current working directory of the process to the given path
See Dir.chdir
CREDIT: Ryan Duryea
# File lib/standard/facets/pathname/chdir.rb, line 10 def chdir(&block) Dir.chdir(self, &block) end
Is a directory path empty?
Returns [Boolean]
# File lib/standard/facets/pathname/empty.rb, line 6 def empty? Dir.glob(::File.join(to_s, '*')).empty? end
Glob pathnames.
# File lib/standard/facets/pathname/glob.rb, line 4 def glob(match, *opts) flags = glob_flags(opts) Dir.glob(::File.join(self.to_s, match), flags).collect{ |m| self.class.new(m) } end
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?
# File lib/standard/facets/pathname/glob.rb, line 14 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
Return globbed matches with pathnames relative to the current pathname.
# File lib/standard/facets/pathname/glob.rb, line 21 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
Does a directory contain a matching entry? Or if the pathname is a file, same as fnmatch.
TODO: Move to own file? Better name?
Returns [Pathname]
# File lib/standard/facets/pathname/glob.rb, line 35 def include?(pattern,*opts) if directory? glob_first(pattern,*opts) else fnmatch(pattern,*opts) end end
Is a path out of date relative a set of source files.
Returns [Boolean]
# File lib/standard/facets/pathname/outofdate.rb, line 9 def outofdate?(*sources) ::FileUtils.outofdate?(to_s, sources.flatten) end
Reads the first line of the file
Captures the best practice from this post at stack overflow: stackoverflow.com/questions/1490138/reading-the-first-line-of-a-file-in-ruby
Credit: Ryan Duryea
# File lib/standard/facets/pathname/readline.rb, line 8 def readline(*args) open { |f| f.readline(*args) } end
# File lib/standard/facets/pathname/rootname.rb, line 6 def rootname self.class.new(File.rootname(to_s)) end
Is a path reasonably safe?
Do not mistake this for a perfect solution!
Returns [Boolean]
# File lib/standard/facets/pathname/safe.rb, line 11 def safe? FileTest.safe?(to_s) end
# File lib/standard/facets/pathname/split_root.rb, line 4 def split_root head, tail = *::File.split_root(to_s) [self.class.new(head), self.class.new(tail)] end
Is a path up to date relative to a set of source files?
Returns [Boolean]
# File lib/standard/facets/pathname/uptodate.rb, line 7 def uptodate?(*sources) ::FileUtils.uptodate?(to_s, sources.flatten) end
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.
Examples
# 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
# File lib/standard/facets/pathname/visit.rb, line 23 def visit(options = {:all => false, :hidden => false}) if self.directory? children.each do |entry| next if entry.basename.to_s[0] == "." && !options[:hidden] yield(entry) unless entry.directory? && !options[:all] ##entry.visit(:all => options[:all]) { |sub_entry| yield sub_entry } if entry.directory? entry.visit(:all => options[:all], :hidden => options[:hidden]) do |sub_entry| yield(sub_entry) end if entry.directory? end else yield self end end
Private Instance Methods
# File lib/standard/facets/pathname/glob.rb, line 45 def glob_flags(opts) flags = 0 opts.each do |opt| case opt when Symbol, String flags += ::File.const_get("FNM_#{opt}".upcase) else flags += opt end end flags end