class DirTravel::Travel

Create directory recursion tree (with {Travel.filetree}). Optionally filter with suffix and modify tree building with options Hash (see below).

If basedir is absolute path, the root name is the search path. If basedir is relative path, before search the current directory is changed to referenced directory and root name is the current directory, single level.

Parameters:

sort

Sort directory entries (default: false).

suffix

Limit search to files with “suffix” (default: nil).

files

Include files to search (default: true).

inclusive

Basedirs parent becomes the basedir (default: false).

Example:

d1 = DirTravel::Travel.filetree( dir1, { :sort => true, :suffix => '.mp3' } )

Attributes

abspath[RW]
basedir[RW]

Starting directory for {Travel}.

defaults[RW]

Default options for {Travel}.

root[RW]

Root {DirEntry} of {Travel}.

Public Class Methods

filetree( basedir = '.', options = {} ) click to toggle source

Create directory recursion tree and return root.

@param basedir [String] Starting directory (top). @param options [Hash] {Travel} options. @return [DirEntry] Root of the file travel hierarchy.

# File lib/dirtravel.rb, line 240
def Travel.filetree( basedir = '.', options = {} )

    # Non-nil if directory needs to be changed.
    pwd = nil

    if basedir[0] == '/'

        # Absolue path.
        t = Travel.new( basedir, basedir, options )
        t.travel

    else

        # Relative path.

        # Store current directory before chdir.
        pwd = Dir.pwd

        # Generate target directory for chdir. One up from
        # the reference.
        full = File.absolute_path( basedir )
        base = File.basename( full )
        dir = File.dirname( full )

        # Goto target.
        Dir.chdir( dir )

        t = Travel.new( base, full, options )
        t.travel
    end


    if t.defaults[ :inclusive ]

        # With inclusive the root is changed to one-up from
        # target.

        # One up from root.
        uppath = File.dirname( t.root.abspath )
        if t.root.relative?
            path = File.basename( uppath )
        else
            path = uppath
        end

        # Create the "one-up" root.
        newRoot = DirEntry.new( path, uppath )

        # Rename old root.
        t.root.rename( t.root.tip )

        # Add to one-up root.
        newRoot.add( t.root )

        # Set root to one-up root.
        t.root = newRoot
    end

    # Return back to start directory if dir changed.
    Dir.chdir( pwd ) if pwd

    # Return root {DirEntry}.
    t.root
end
new( basedir, abspath, options = {} ) click to toggle source

Create directory recursion object. Overlay options on top of defaults. Initialize root {DirEntry}.

@param basedir [String] Starting directory (top). @param abspath [String] Absolute path for root. @param options [Hash] {Travel} options.

# File lib/dirtravel.rb, line 312
def initialize( basedir, abspath, options = {} )
    @basedir = basedir
    @abspath = abspath

    @defaults = {
        :suffix    => nil,
        :sort      => false,
        :files     => true,
        :inclusive => false,
    }

    @defaults.merge!( options )

    @root = DirEntry.new( basedir, abspath )
end

Public Instance Methods

travel( suffix = @defaults[ :suffix ] ) click to toggle source

Recursively get all files with suffix. Ignore suffix if suffix is nil.

# File lib/dirtravel.rb, line 331
def travel( suffix = @defaults[ :suffix ] )
    entriesIn( @basedir, @root, suffix )
end

Private Instance Methods

entries( dir ) click to toggle source

Directory entries with dot entries filtered out.

# File lib/dirtravel.rb, line 371
def entries( dir )
    entries = Dir.entries( dir ).select do |i| nonDotEntry( i ); end
    entries.sort! if @defaults[ :sort ]
    entries
end
entriesIn( dir, node, suffix = nil ) click to toggle source

Recursively get all files with suffix. Ignore suffix if suffix is nil.

# File lib/dirtravel.rb, line 340
def entriesIn( dir, node, suffix = nil )

    list = entries( dir )

    list.each do |i|

        if File.file?( dir + '/' + i )

            # File entry.

            if ( !suffix || suffix == File.extname( i ) ) &&
                    @defaults[ :files ]
                node.add( FileEntry.new( i ) )
            end

        else

            # Dir entry.

            newNode = DirEntry.new( i )
            node.add( newNode )
            entriesIn( dir + '/' + i, newNode, suffix )

        end

    end

    self
end
nonDotEntry( name ) click to toggle source

Test for non-dot-entry.

# File lib/dirtravel.rb, line 378
def nonDotEntry( name )
    ( name != '.' && name != '..' )
end