Class: Pathname
- Defined in:
- lib/standard/facets/pathname/glob.rb,
lib/standard/facets/pathname/home.rb,
lib/standard/facets/pathname/null.rb,
lib/standard/facets/pathname/root.rb,
lib/standard/facets/pathname/safe.rb,
lib/standard/facets/pathname/work.rb,
lib/standard/facets/pathname/chdir.rb,
lib/standard/facets/pathname/visit.rb,
lib/standard/facets/pathname/exists.rb,
lib/standard/facets/pathname/op_div.rb,
lib/standard/facets/pathname/to_str.rb,
lib/standard/facets/pathname/op_fetch.rb,
lib/standard/facets/pathname/readline.rb,
lib/standard/facets/pathname/rootname.rb,
lib/standard/facets/pathname/uptodate.rb,
lib/standard/facets/pathname/outofdate.rb,
lib/standard/facets/pathname/split_root.rb
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
deprecated
Deprecated.
Use Pathname.new(File::NULL) instead.
-
.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
-
#chdir(&block) ⇒ Object
Change current working directory of the process to the given path.
- #glob(match, *opts) ⇒ Object
-
#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
Is a path out of date relative a set of source files.
-
#readline(*args) ⇒ Object
Reads the first line of the file.
- #rootname ⇒ Object
-
#safe? ⇒ Boolean
Is a path reasonably safe?.
- #split_root ⇒ Object
-
#uptodate?(*sources) ⇒ Boolean
Is a path up to date relative to a set of source files?.
-
#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'
7 8 9 |
# File 'lib/standard/facets/pathname/op_div.rb', line 7 def self./(path) new(path) end |
.[](path) ⇒ Object
9 10 11 |
# File 'lib/standard/facets/pathname/op_fetch.rb', line 9 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.
Returns [Pathname]
8 9 10 |
# File 'lib/standard/facets/pathname/home.rb', line 8 def self.home Pathname.new('~') end |
.null ⇒ Object
Use Pathname.new(File::NULL) instead.
4 5 6 7 |
# File 'lib/standard/facets/pathname/null.rb', line 4 def self.null warn "Pathname.null is deprecated. Use Pathname.new(File::NULL) instead.", uplevel: 1 new(File::NULL) end |
Instance Method Details
#chdir(&block) ⇒ Object
Change current working directory of the process to the given path
See Dir.chdir
CREDIT: Ryan Duryea
10 11 12 |
# File 'lib/standard/facets/pathname/chdir.rb', line 10 def chdir(&block) Dir.chdir(self, &block) end |
#glob(match, *opts) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/standard/facets/pathname/glob.rb', line 13 def glob(match, *opts) if opts.any? { |o| o.is_a?(Symbol) || o.is_a?(String) } flags = glob_flags(opts) Dir.glob(::File.join(self.to_s, match), flags).map { |m| self.class.new(m) } else _glob_original(match, *opts) end end |
#glob_first(match, *opts) ⇒ Object
Return the first glob match.
24 25 26 27 28 |
# File 'lib/standard/facets/pathname/glob.rb', line 24 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.
31 32 33 34 35 36 |
# File 'lib/standard/facets/pathname/glob.rb', line 31 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.
Returns [Pathname]
42 43 44 45 46 47 48 |
# File 'lib/standard/facets/pathname/glob.rb', line 42 def include?(pattern, *opts) if directory? glob_first(pattern, *opts) else fnmatch(pattern, *opts) end end |
#outofdate?(*sources) ⇒ Boolean
Is a path out of date relative a set of source files.
Returns [Boolean]
9 10 11 |
# File 'lib/standard/facets/pathname/outofdate.rb', line 9 def outofdate?(*sources) ::FileUtils.outofdate?(to_s, sources.flatten) end |
#readline(*args) ⇒ Object
Reads the first line of the file
Captures the best practice from this post at stack overflow: https://stackoverflow.com/questions/1490138/reading-the-first-line-of-a-file-in-ruby
Credit: Ryan Duryea
8 9 10 |
# File 'lib/standard/facets/pathname/readline.rb', line 8 def readline(*args) open { |f| f.readline(*args) } end |
#rootname ⇒ Object
6 7 8 |
# File 'lib/standard/facets/pathname/rootname.rb', line 6 def rootname self.class.new(File.rootname(to_s)) end |
#safe? ⇒ Boolean
Is a path reasonably safe?
Do not mistake this for a perfect solution!
Returns [Boolean]
11 12 13 |
# File 'lib/standard/facets/pathname/safe.rb', line 11 def safe? FileTest.safe?(to_s) end |
#split_root ⇒ Object
4 5 6 7 |
# 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 |
#uptodate?(*sources) ⇒ Boolean
Is a path up to date relative to a set of source files?
Returns [Boolean]
7 8 9 |
# File 'lib/standard/facets/pathname/uptodate.rb', line 7 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
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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/standard/facets/pathname/visit.rb', line 23 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 |