Active path separator.
p1 = Pathname.new('/') p2 = p1 / 'usr' / 'share' #=> Pathname:/usr/share
# File lib/more/facets/pathname.rb, line 45 45: def self./(path) 46: new(path) 47: end
Alternate to Pathname#new.
Pathname['/usr/share']
# File lib/more/facets/pathname.rb, line 36 36: def self.[](path) 37: new(path) 38: end
Home constant for building paths from root directory onward.
TODO: Pathname#home needs to be more robust.
# File lib/more/facets/pathname.rb, line 58 58: def self.home 59: Pathname.new('~') 60: end
Platform dependent null device.
# File lib/more/facets/pathname.rb, line 75 75: def self.null 76: case RUBY_PLATFORM 77: when /mswin/ 78: 'NUL' 79: when /amiga/ 80: 'NIL:' 81: when /openvms/ 82: 'NL:' 83: else 84: '/dev/null' 85: end 86: end
# File lib/more/facets/pathname.rb, line 141 141: def empty? 142: Dir.glob(::File.join(to_s, '*')).empty? 143: end
Glob pathnames.
# File lib/more/facets/pathname.rb, line 100 100: def glob(match, *opts) 101: flags = glob_flags(opts) 102: Dir.glob(::File.join(self.to_s, match), flags).collect{ |m| self.class.new(m) } 103: 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/more/facets/pathname.rb, line 110 110: def glob_first(match, *opts) 111: flags = glob_flags(opts) 112: file = ::Dir.glob(::File.join(self.to_s, match), flags).first 113: file ? self.class.new(file) : nil 114: end
Return globbed matches with pathnames relative to the current pathname.
# File lib/more/facets/pathname.rb, line 117 117: def glob_relative(match, *opts) 118: flags = glob_flags(opts) 119: files = Dir.glob(::File.join(self.to_s, match), flags) 120: files = files.map{ |f| f.sub(self.to_s.chomp('/') + '/', '') } 121: files.collect{ |m| self.class.new(m) } 122: end
# File lib/more/facets/pathname.rb, line 151 151: def outofdate?(*sources) 152: ::FileUtils.outofdate?(to_s, sources.flatten) 153: end
# File lib/more/facets/pathname.rb, line 89 89: def rootname 90: self.class.new(File.rootname(to_s)) 91: end
# File lib/more/facets/pathname.rb, line 94 94: def split_root 95: head, tail = *::File.split_root(to_s) 96: [self.class.new(head), self.class.new(tail)] 97: end
# File lib/more/facets/pathname.rb, line 146 146: def uptodate?(*sources) 147: ::FileUtils.uptodate?(to_s, sources.flatten) 148: 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.
Example use case:
# get rid of any file but *.haml within app/**/* Pathname.new("app").visit do |f| FileUtils.rm_f(f) unless f.to_s =~ /\.haml$/ puts "!!! deleting #{f}" end
CREDIT: Jean-Denis Vauguet
# File lib/more/facets/pathname.rb, line 172 172: def visit(options = {:all => false, :hidden => false}) 173: if self.directory? 174: children.each do |entry| 175: next if entry.basename.to_s[0] == "." && !options[:hidden] 176: yield(entry) unless entry.directory? && !options[:all] 177: #entry.visit(:all => options[:all]) { |sub_entry| yield sub_entry } if entry.directory? 178: entry.visit(:all => options[:all], :hidden => options[:hidden]) do |sub_entry| 179: yield(sub_entry) 180: end if entry.directory? 181: end 182: else 183: yield self 184: end 185: end
# File lib/more/facets/pathname.rb, line 126 126: def glob_flags(opts) 127: flags = 0 128: opts.each do |opt| 129: case opt when Symbol, String 130: flags += ::File.const_get("FNM_#{opt}".upcase) 131: else 132: flags += opt 133: end 134: end 135: flags 136: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.