lib/dirtravel.rb in dirtravel-0.0.4 vs lib/dirtravel.rb in dirtravel-0.0.5
- old
+ new
@@ -159,19 +159,15 @@
class DirEntry < Entry
# Instantiate.
#
# @param name [String] Directory name.
- # @param root [Boolean] If root, abspath is set now.
- def initialize( name, root = false )
+ # @param abspath [String] Set abspath if given.
+ def initialize( name, abspath = nil )
super( name )
- if root
- if name[0] == '/'
- @abspath = name
- else
- @abspath = Dir.pwd + '/' + name
- end
+ if abspath
+ @abspath = abspath
end
end
end
@@ -212,51 +208,67 @@
attr_accessor :root
# Default options for Travel.
attr_accessor :defaults
- # Starting directory for Travel
+ # Starting directory for Travel.
attr_accessor :basedir
+ attr_accessor :abspath
# Create directory recursion tree.
# @param basedir [String] Starting directory (top).
# @param options [Hash] Hash optionally including keys: :sort, :suffix, :files.
# @return [DirEntry] Root item of the file system hierarchy.
def Travel.filetree( basedir = '.', options = {} )
- r = Travel.new( basedir, options )
- r.travel
+ pwd = nil
- if r.defaults[ :inclusive ]
- path = File.dirname( r.root.abspath )
- if r.root.relative?
- path = File.basename( path )
+ if basedir[0] == '/'
+ t = Travel.new( basedir, basedir, options )
+ t.travel
+ else
+ pwd = Dir.pwd
+ full = File.absolute_path( basedir )
+ base = File.basename( full )
+ dir = File.dirname( full )
+ Dir.chdir( dir )
+ t = Travel.new( base, full, options )
+ t.travel
+ end
+
+ if t.defaults[ :inclusive ]
+ uppath = File.dirname( t.root.abspath )
+ path = uppath
+ if t.root.relative?
+ path = File.basename( uppath )
end
- newRoot = DirEntry.new( path, true )
- r.root.rename( r.root.tip )
- newRoot.add( r.root )
- r.root = newRoot
+ newRoot = DirEntry.new( path, uppath )
+ t.root.rename( t.root.tip )
+ newRoot.add( t.root )
+ t.root = newRoot
end
- r.root
+ Dir.chdir( pwd ) if pwd
+
+ t.root
end
- def initialize( basedir = '.', options = {} )
+ def initialize( basedir, abspath, options = {} )
@basedir = basedir
+ @abspath = abspath
@defaults = {
:suffix => nil,
:sort => false,
:files => true,
:inclusive => false,
}
@defaults.merge!( options )
- @root = DirEntry.new( clean( @basedir ), true )
-
+ @root = DirEntry.new( basedir, abspath )
end
# Recursively get all files with suffix. Ignore suffix if
# suffix is nil.
@@ -264,19 +276,9 @@
entriesIn( @basedir, @root, suffix )
end
private
-
- # Clean relative path head (remove dot etc).
- def clean( dir )
- if dir[0] == '/'
- dir
- else
- File.basename( File.absolute_path( dir ) )
- end
- end
-
# Recursively get all files with suffix. Ignore suffix if
# suffix is nil.
def entriesIn( dir, node, suffix = nil )