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

- old
+ new

@@ -1,318 +1,294 @@ -class Pa - NAME_EXT_PAT = /^(.+?)(?:\.([^.]+))?$/ -module ClassMethods::Path +=begin - # alias from File.absolute_path - # @param [String,Pa] path - # @return [String] - def absolute(path); File.absolute_path(get(path)) end +attribute absolute and dir return String, method absolute_path(), dirname() return Pa + + Pa("/home/a").dir #=> "/home" + Pa("/home/a").dirname #=> Pa("/home") - # alias from File.expand_path - # @param [String,Pa] path - # @return [String] - def expand(path); File.expand_path(get(path)) end +== methods from String +* + +* [g]sub[!] match =~ +* start_with? end_with? - # shorten a path, - # convert /home/user/file to ~/file - # - # @param [String,Pa] path - # @return [String] - def shorten(path); - get(path).sub(%r!^#{Regexp.escape(ENV["HOME"])}!, "~") - end +=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 + # + # @param [String,#path] obj + # @return [String,nil] path + def get(obj) + if obj.respond_to?(:path) + obj.path + elsif String === obj + obj + else + raise Error, "not support type -- #{obj.inspect}(#{obj.class})" + end + end - # return current work directory - # @return [String] path - def pwd() Dir.getwd end + # return current work directory + # @return [String] path + def pwd2 + Dir.getwd + end - # @return [Pa] path - def pwd2() Pa(Dir.getwd) end + def dir2(path) + File.dirname(path) + end - # change directory - # - # @param [String,Pa] path - def cd(path=ENV["HOME"], &blk) Dir.chdir(get(path), &blk) end + # get a basename of a path + # + # @example + # Pa.basename("foo.bar.c", ext: true) #=> \["foo.bar", "c"] + # + # @param [String,Pa] name + # @param [Hash] o options + # @option o [Boolean, String] :ext (false) return \[name, ext] if true + # + # @return [String] basename of a path unless o[:ext] + # @return [Array<String>] \[name, ext] if o[:ext]. + def base2(name, o={}) + name = File.basename(get(name)) + if o[:ext] + name, ext = name.match(NAME_EXT_PAT).captures + [ name, (ext || "")] + else + name + end + end - # get path of an object. - # - # return obj#path if object has a 'path' instance method - # - # @param [String,#path] obj - # @return [String,nil] path - def get obj - if obj.respond_to?(:path) - obj.path - elsif String === obj - obj - else - raise Error, "not support type -- #{obj.inspect}(#{obj.class})" - end - end + # ext of a path + # + # @example + # "a.ogg" => "ogg" + # "a" => nil + # + # @param [String,Pa] path + # @return [String] + def ext2 path + _, ext = get(path).match(/\.([^.]+)$/).to_a + ext + end - # extname of a path - # - # @example - # "a.ogg" => "ogg" - # "a" => nil - # - # @param [String,Pa] path - # @return [String] - def extname path - _, ext = get(path).match(/\.([^.]+)$/).to_a - ext - end - # is path an absolute path ? - # - # @param [String,Pa] path - # @return [Boolean] - def absolute?(path) path=get(path); File.absolute_path(path) == path end + # alias from File.absolute_path + # @param [String,Pa] path + # @return [String] + def absolute2(path); File.absolute_path(get(path)) end - # get a basename of a path - # - # @example - # Pa.basename("foo.bar.c", ext: true) #=> \["foo.bar", "c"] - # - # @param [String,Pa] name - # @param [Hash] o options - # @option o [Boolean, String] :ext (false) return \[name, ext] if true - # - # @return [String] basename of a path unless o[:ext] - # @return [Array<String>] \[name, ext] if o[:ext]. - def basename(name, o={}) - name = File.basename(get(name)) - if o[:ext] - name, ext = name.match(NAME_EXT_PAT).captures - [ name, (ext || "")] - else - name - end - end - - # split path - # - # @example - # path="/home/a/file" - # split(path) #=> "/home/a", "file" - # split(path, :all) #=> "/", "home", "a", "file" - # - # @param [String,Pa] name - # @param [Hash] o option - # @option o [Boolean] :all split all parts - # @return [Array<String>] - def split(name, o={}) - dir, fname = File.split(get(name)) - ret = Array.wrap(basename(fname, o)) + # alias from File.expand_path + # @param [String,Pa] path + # @return [String] + def expand2(path); File.expand_path(get(path)) end - if o[:all] - loop do - dir1, fname = File.split(dir) - break if dir1 == dir - ret.unshift fname - dir = dir1 - end - end - ret.unshift dir - ret - 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"])}!, "~") + end - # join paths, skip nil and empty string. - # - # @param [*Array<String>] *paths - # @return [String] - def join *paths - paths.map!{|v|get(v)} - # skip nil - paths.compact! - # skip empty string - paths.delete("") + # is path an absolute path ? + # + # @param [String,Pa] path + # @return [Boolean] + def absolute?(path) path=get(path); File.absolute_path(path) == path end + + # split path + # + # @example + # path="/home/a/file" + # split(path) #=> "/home/a", "file" + # split(path, :all) #=> "/", "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)) - File.join(*paths) - end + if o[:all] + loop do + dir1, fname = File.split(dir) + break if dir1 == dir + ret.unshift fname + dir = dir1 + end + end + ret.unshift dir + ret + end - # get parent path - # - # @param [String,Pa] path - # @param [Fixnum] n up level - # @return [String] - def parent path, n=1 - path = get(path) - n.times do - path = File.dirname(path) - end - path - end + def split(*args) + dir, *names = split2(*args) + [ Pa(dir), *names] + end - # link - # - # @overload ln(src, dest) - # @overload ln([src,..], directory) - # - # @param [Array<String>, String] src_s support globbing - # @param [String,Pa] dest - # @param [Hash] o option - # @option o [Boolean] :force overwrite if exists. - # @return [nil] - def ln(src_s, dest, o={}) _ln(File.method(:link), src_s, dest, o) end + # join paths, skip nil and empty string. + # + # @param [*Array<String>] *paths + # @return [String] + def join2(*paths) + paths.map!{|v|get(v)} - # ln force - # - # @see ln - # @return [nil] - def ln_f(src_s, dest, o) o[:force]=true; _ln(File.method(:link), src_s, dest, o) end + # skip nil + paths.compact! - # symbol link - # - # @see ln - # @return [nil] - def symln(src_s, dest, o) _ln(File.method(:symlink), src_s, dest, o) end - alias symlink ln + # skip empty string + paths.delete("") - # symln force - # - # @see ln - # @return [nil] - def symln_f(src_s, dest, o) o[:force]=true; _ln(File.method(:symlink), src_s, dest, o) end + File.join(*paths) + end - # @param [Array,String,#path] src_s - # @param [String,#path] dest - def _ln(method, src_s, dest, o={}) - dest = get(dest) - glob(*Array.wrap(src_s)) {|src| - src = get(src) - dest = File.join(dest, File.basename(src)) if File.directory?(dest) - Pa.rm_r(dest) if o[:force] and File.exists?(dest) - method.call(src, dest) - } - end - private :_ln + # 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 - # @see File.readlink - def readlink(path) File.readlink(get(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? - # 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 - def realpath(path) File.realpath(get(path)) end + module InstanceMethods + # @return [String] + attr_reader :absolute2, :dir2, :base2, :name2, :short2 -end -end + # @return [String] ext "", "ogg" + attr_reader :ext2 -class Pa -=begin + # @return [String] ext "", ".ogg" + attr_reader :fext2 -attribute absolute and dir return String, method absolute_path(), dirname() return Pa - - Pa("/home/a").dir #=> "/home" - Pa("/home/a").dirname #=> Pa("/home") + def initialize_variables + super + @absolute2 = File.absolute_path(@path) + @dir2 = File.dirname(@path) + @base2 = File.basename(@path) + @name2, @ext2 = @base2.match(NAME_EXT_PAT).captures + @ext2 ||= "" + @fext2 = @ext2.empty? ? "" : "."+@ext2 + end -== methods from String -* + -* [g]sub[!] match =~ -* start_with? end_with? + alias a2 absolute2 + alias d2 dir2 + alias b2 base2 + alias n2 name2 + alias fname2 base2 + alias fn2 fname2 + alias e2 ext2 + alias fe2 fext2 -=end -module Path - # @return [String] - attr_reader :absolute, :dir, :base, :name, :short + # fix name,2 => String + alias name name2 + alias fname fname2 + alias ext ext2 + alias fext fext2 - # @return [String] ext "", "ogg" - attr_reader :ext + alias fn fname + alias n name + alias e ext + alias fe fext - # @return [String] ext "", ".ogg" - attr_reader :fext + def short2 + @short2 ||= Pa.shorten2(@path) + end - attr_reader :absolute_pa, :dir_pa, :base_pa, :name_pa, :ext_pa, :fext_pa, :short_pa + # add string to path + # + # @example + # pa = Pa('/home/foo/a.txt') + # pa+'~' #=> new Pa('/home/foo/a.txt~') + # + # @param [String] str + # @return [Pa] + def +(str) + Pa(path+str) + end - def initialize_variables - super - @absolute = File.absolute_path(@path) - @dir = File.dirname(@path) - @base = File.basename(@path) - @name, @ext = @base.match(NAME_EXT_PAT).captures - @ext ||= "" - @fext = @ext.empty? ? "" : "."+@ext - end + # @return [String] + def sub2(*args,&blk) + path.sub(*args,&blk) + end - def absolute_pa; @absolute_pa ||= Pa(@absolute) end - def dir_pa; @dir_pa ||= Pa(@dir) end - def base_pa; @base_pa ||= Pa(@base) end - def name_pa; @name_pa ||= Pa(@name) end - def ext_pa; @ext_pa ||= Pa(@ext) end - def fext_pa; @fext_pa ||= Pa(@fext) end - def short_pa; @short_pa ||= Pa(@short) end + # @return [String] + def gsub2(*args,&blk) + path.gsub(*args,&blk) + end - alias a absolute - alias d dir - alias b base - alias n name - alias fname base - alias fn fname - alias e ext - alias fe fext + # @return [Pa] + def sub!(*args,&blk) + self.replace path.sub(*args,&blk) + end - alias a_pa absolute_pa - alias d_pa dir_pa - alias b_pa base_pa - alias n_pa name_pa - alias fname_pa base_pa - alias fn_pa fname_pa - alias e_pa ext_pa - alias fe_pa fext_pa + # @return [Pa] + def gsub!(*args,&blk) + self.replace path.gsub(*args,&blk) + end - def short - @short ||= Pa.shorten(@path) - end + # @return [MatchData] + def match(*args,&blk) + path.match(*args,&blk) + end - # add string to path - # - # @example - # pa = Pa('/home/foo/a.txt') - # pa+'~' #=> new Pa('/home/foo/a.txt~') - # - # @param [String] str - # @return [Pa] - def +(str) Pa(path+str) end + # @return [Boolean] + def start_with?(*args) + path.start_with?(*args) + end - # @return [Pa] - def sub(*args,&blk) Pa(path.sub(*args,&blk)) end + # @return [Boolean] + def end_with?(*args) + path.end_with?(*args) + end - # @return [Pa] - def gsub(*args,&blk) Pa(path.gsub(*args,&blk)) end + def =~(regexp) + path =~ regexp + end - # @return [Pa] - def sub!(*args,&blk) self.replace path.sub(*args,&blk) end - - # @return [Pa] - def gsub!(*args,&blk) self.replace path.gsub(*args,&blk) end - - # @return [MatchData] - def match(*args,&blk) path.match(*args,&blk) end - - # @return [Boolean] - def start_with?(*args) path.start_with?(*args) end - - # @return [Boolean] - def end_with?(*args) path.end_with?(*args) end - - def =~(regexp) path =~ regexp end - - def ==(other) self.path == other.path end -end + def ==(other) + case other + when Pa + self.path == other.path + else + false + end + end + end + end end