lib/pa.rb in pa-1.2.3 vs lib/pa.rb in pa-1.3.0

- old
+ new

@@ -1,6 +1,7 @@ require "tmpdir" +require "pd" =begin rdoc Pa(Path) is similary to Pathname, but more powerful. it combines fileutils, tmpdir, find, tempfile, File, Dir, Pathname @@ -202,14 +203,15 @@ path end end DELEGATE_METHODS2 = [ :join2 ] - DELEGATE_METHODS = [ :dir, :build, :join ] + DELEGATE_METHODS = [ :build, :join ] + DELEGATE_ATTR_METHODS = [ :dir, :rel, :rea ] attr_reader :path2 - attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel, :rea + attr_reader :absolute2, :dir2, :dir_strict2, :base2, :fname2, :name2, :short2, :ext2, :fext2, :rel2, :rea2 attr_reader :options # @param [Hash] o option # @option o [String] rel relative path # @option o [String] base_dir @@ -218,70 +220,72 @@ @path2 = Pa.get(path) # convert ~ to ENV["HOME"] @path2.sub!(/^~/, ENV["HOME"].to_s) if @path2 # nil @options = o - @rel = o[:rel] || "" - @base_dir = o[:base_dir] + @base_dir = o[:base_dir] || "." initialize_variables end chainable = Module.new do def initialize_variables; end end include chainable - def rel - raise Error, "don't have a :rel option" unless rel? - - @rel ||= options[:rel] - end - def base_dir - raise Error, "don't have a :base_dir option" unless base_dir? - - @base_dir ||= options[:base_dir] + @base_dir ||= (options[:base_dir] || ".") end - def rea - raise Error, "don't have a :base_dir option" unless base_dir? - - @rea ||= File.join(options[:base_dir], path) + def rel2 + @rel2 ||= (options[:rel] || "" ) end - def rel? - options.has_key?(:rel) + def rea2 + @rea2 ||= options[:base_dir] ? File.join(base_dir, path) : path end - def base_dir? - options.has_key?(:base_dir) + def absolute2 + @absolute2 ||= File.absolute_path(rea2) end - def absolute2 - @absolute2 ||= File.absolute_path(options[:base_dir] ? rea : path) + def absolute + @absolute ||= Pa(absolute2) end # => ".", "..", "/", "c:" + # + # "foo" => "." + # "./foo" => "." + # "../../foo" => "../.." + # def dir2 @dir2 ||= File.dirname(path) end + def dir + @dir ||= Pa(dir2) + end + # Pa("foo") => "" # Pa("./foo") => "." def dir_strict2 return @dir_strict2 if @dir_strict2 dir = File.dirname(path) - @dir_strict2 = if %w[. ..].include?(dir) && path !~ %r!^\.\.?/! + @dir_strict2 = if %w[.].include?(dir) && path !~ %r~^\./~ "" else dir end end + def dir_strict + @dir_strict ||= Pa(dir_strict2) + end + def base2 @base2 ||= File.basename(path) end def name2 @@ -306,11 +310,11 @@ alias fname fname2 alias name name2 alias ext ext2 alias fext fext2 - # abbretive + # abbreviate alias p2 path2 alias p2 path alias a2 absolute2 alias d2 dir2 alias d_s2 dir_strict2 @@ -324,10 +328,14 @@ alias fn fname alias n name alias e ext alias fe fext + alias a absolute + alias d dir + alias d_s dir_strict + # return '#<Pa @path="foo", @absolute="/home/foo">' # # @return [String] def inspect ret="#<" + self.class.to_s + " " @@ -381,10 +389,14 @@ def short2 @short2 ||= Pa.shorten2(@path) end + def short + @short ||= Pa(short2) + end + # @return [String] def sub2(*args, &blk) path.sub(*args, &blk) end @@ -456,10 +468,18 @@ def #{mth}(*args, &blk) Pa(#{mth}2(*args, &blk)) end EOF } + + DELEGATE_ATTR_METHODS.each {|mth| + class_eval <<-EOF + def #{mth}(*args, &blk) + @#{mth} ||= Pa(#{mth}2(*args, &blk)) + end + EOF + } end require "pa/path" require "pa/cmd" require "pa/directory" @@ -474,11 +494,12 @@ module Kernel private # a very convient function. # # @example + # # Pa('/home').exists? - def Pa(path) + def Pa(path, o={}) return path if Pa===path - Pa.new path + Pa.new path, o end end