lib/pa/path.rb in pa-1.1.3 vs lib/pa/path.rb in pa-1.1.4

- old
+ new

@@ -13,33 +13,65 @@ =end class Pa NAME_EXT_PAT = /^(.+?)(?:\.([^.]+))?$/ module Path extend Util::Concern + module ClassMethods # get path of an object. # # return obj#path if object has a 'path' instance method # + # nil -> nil + # + # # @param [String,#path] obj # @return [String,nil] path def get(obj) if obj.respond_to?(:path) obj.path elsif String === obj obj + elsif obj.nil? + nil else - raise Error, "not support type -- #{obj.inspect}(#{obj.class})" + raise ArgumentError, "Pa.get() not support type -- #{obj.inspect}(#{obj.class})" end end # return current work directory # @return [String] path def pwd2 Dir.getwd end + # is path an absolute path ? + # + # @param [String,Pa] path + # @return [Boolean] + def absolute?(path) + path=get(path) + File.absolute_path(path) == path + end + + # is path a dangling symlink? + # + # a dangling symlink is a dead symlink. + # + # @param [String,Pa] path + # @return [Boolean] + def dangling? path + path=get(path) + if File.symlink?(path) + src = File.readlink(path) + not File.exists?(src) + else + nil + end + end # def dsymlink? + + def dir2(path) File.dirname(path) end # get a basename of a path @@ -74,52 +106,65 @@ def ext2 path _, ext = get(path).match(/\.([^.]+)$/).to_a ext end - # alias from File.absolute_path # @param [String,Pa] path # @return [String] - def absolute2(path); File.absolute_path(get(path)) end + def absolute2(path) + File.absolute_path get(path) + end # alias from File.expand_path # @param [String,Pa] path # @return [String] - def expand2(path); File.expand_path(get(path)) end + def expand2(path) + File.expand_path get(path) + end # shorten2 a path, # convert /home/user/file to ~/file # # @param [String,Pa] path # @return [String] - def shorten2(path); - get(path).sub(%r!^#{Regexp.escape(ENV["HOME"])}!, "~") + def shorten2(path) + get(path).sub /^#{Regexp.escape(ENV["HOME"])}/, "~" end + # real path + def real2(path) + File.realpath get(path) + end - - # is path an absolute path ? - # + # get parent path + # # @param [String,Pa] path - # @return [Boolean] - def absolute?(path) path=get(path); File.absolute_path(path) == path end + # @param [Fixnum] n up level + # @return [String] + def parent2(path, n=1) + path = get(path) + n.times do + path = File.dirname(path) + end + path + end # split path # # @example # path="/home/a/file" - # split(path) #=> "/home/a", "file" - # split(path, :all) #=> "/", "home", "a", "file" + # split2(path) #=> "/home/a", "file" + # split2(path, :all => true) #=> "/", "home", "a", "file" # # @param [String,Pa] name # @param [Hash] o option # @option o [Boolean] :all split all parts # @return [Array<String>] def split2(name, o={}) dir, fname = File.split(get(name)) - ret = Util.wrap_array(basename(fname, o)) + ret = Util.wrap_array(File.basename(fname)) if o[:all] loop do dir1, fname = File.split(dir) break if dir1 == dir @@ -129,10 +174,11 @@ end ret.unshift dir ret end + # special case def split(*args) dir, *names = split2(*args) [ Pa(dir), *names] end @@ -149,42 +195,10 @@ # skip empty string paths.delete("") File.join(*paths) end - - # get parent path - # - # @param [String,Pa] path - # @param [Fixnum] n up level - # @return [String] - def parent2(path, n=1) - path = get(path) - n.times do - path = File.dirname(path) - end - path - end - - # is path a dangling symlink? - # - # a dangling symlink is a dead symlink. - # - # @param [String,Pa] path - # @return [Boolean] - def dangling? path - path=get(path) - if File.symlink?(path) - src = File.readlink(path) - not File.exists?(src) - else - nil - end - end # def dsymlink? - - # real path - def real2(path) File.realpath(get(path)) end end module InstanceMethods # @return [String] attr_reader :absolute2, :dir2, :base2, :name2, :short2 @@ -213,15 +227,17 @@ alias fn2 fname2 alias e2 ext2 alias fe2 fext2 # fix name,2 => String - alias name name2 + alias base base2 alias fname fname2 + alias name name2 alias ext ext2 alias fext fext2 + alias b base alias fn fname alias n name alias e ext alias fe fext @@ -240,16 +256,26 @@ def +(str) Pa(path+str) end # @return [String] - def sub2(*args,&blk) - path.sub(*args,&blk) + def sub2(*args, &blk) + path.sub(*args, &blk) end # @return [String] - def gsub2(*args,&blk) - path.gsub(*args,&blk) + def gsub2(*args, &blk) + path.gsub(*args, &blk) + end + + # @return [Pa] + def sub(*args, &blk) + Pa(sub2(*args, &blk)) + end + + # @return [Pa] + def gsub(*args, &blk) + Pa(gsub2(*args, &blk)) end # @return [Pa] def sub!(*args,&blk) self.replace path.sub(*args,&blk)