require 'pathname'
require 'find'
module PathUtils
def find(*args)
Find.find(*args) do |path|
yield Pathname.new(path)
end
end
module_function :find
end
class Pathname
def nest_level(base=nil)
lv = 0
path = base ? relative_path_from(base) : self
path.each_filename{|x| lv += 1}
lv -= 1 unless File.directory?(to_str)
lv
end
def root_change(root1, root2)
root2 + relative_path_from(root1)
end
def ext_change(ext)
extension = ext.dup
extension.slice!(/\A\.*/)
name = to_s
name.slice!(/#{extname}\Z/)
Pathname.new("#{name}.#{extension}")
end
def ext_remove
temp = ext_change("").to_s
temp.slice!(/\.\Z/)
Pathname.new(temp)
end
def ext?(other_ext)
ext1 = extname
ext1.slice!(/\A\./)
ext2 = other_ext.dup
ext2.slice!(/\A\./)
ext1 == ext2
end
def slice(regexp)
temp = to_s
temp.slice!(regexp)
Pathname.new(temp)
end
end