lib/shell_helpers/pathname.rb in shell_helpers-0.6.0 vs lib/shell_helpers/pathname.rb in shell_helpers-0.7.0
- old
+ new
@@ -1,6 +1,7 @@
require 'pathname'
+require 'forwardable'
#backports from ruby 2.1
class Pathname
unless method_defined?(:write)
def write(*args,&b)
@@ -95,10 +96,18 @@
#exist? returns false if called on a symlink pointing to a non existing file
def may_exist?
exist? or symlink?
end
+ #like read, but output nil rather than an exception if the file does
+ #not exist
+ def read!
+ read
+ rescue
+ nil
+ end
+
def filewrite(*args,mode:"w",perm: nil,mkpath: false,backup: false)
logger.debug("Write to #{self}"+ (perm ? " (#{perm})" : "")) if respond_to?(:logger)
self.dirname.mkpath if mkpath
self.backup if backup and exist?
if !exist? && symlink?
@@ -267,14 +276,18 @@
def find(*args,&b)
require 'shell_helpers/utils'
Utils.find(self,*args,&b)
end
- def glob(pattern, expand: false)
+ # We now have Pathname#glob
+ def rel_glob(pattern, expand: false)
g=[]
self.cd { g=Dir.glob(pattern) }
- g=g.map {|f| self+f} if expand
- g
+ if expand
+ g.map {|f| self+f}
+ else
+ g.map {|f| Pathname.new(f)}
+ end
end
#follow a symlink
def follow
return self unless symlink?