class Based::Directory

A base class for directories: i.e. either the base directory itself, or a sub-directory

Attributes

base[R]

The base directory (object)

fullPath[R]

The full path of the file

name[R]

The immediate name of the directory (nil for the base directory)

parent[R]

The parent directory (nil for the base directory)

pathElements[R]

The elements of the relative path as an array

relativePath[R]

The path of this directory relative to the base directory (includes a following “/” if non-empty)

Public Class Methods

new() click to toggle source

initialise with un-initialised entries

# File lib/based.rb, line 33
def initialize
  @entries = nil
end

Public Instance Methods

allFiles() click to toggle source

Get a list of all files contained within this directory

# File lib/based.rb, line 91
def allFiles
  result = files
  for subDir in subDirs
    result += subDir.files
  end
  return result
end
dirs() click to toggle source

Get list of directories immediately contained in this directory

# File lib/based.rb, line 75
def dirs
  getEntries()
  return @dirs
end
files() click to toggle source

Get list of files immediately contained in this directory

# File lib/based.rb, line 69
def files
  getEntries()
  return @files
end
getEntries() click to toggle source

get the “entries”, i.e. list of files and directories immediately contained in this directory, and cache them. Note that dirExclude, fileInclude and fileExclude functions in the base directory object may be applied to filter out some entries.

# File lib/based.rb, line 40
def getEntries
  if @entries == nil
    @entries = Dir.entries(fullPath)
    @dirs = []
    @files = []
    for entry in @entries
      if entry != "." and entry != ".." 
        fullEntryPath = fullPath + entry
        if ::File.directory?(fullEntryPath)
          subDirectory = SubDirectory.new(entry, self)
          if @base.dirExclude == nil or not @base.dirExclude.call(subDirectory)
            @dirs << subDirectory
          end
        elsif ::File.file?(fullEntryPath)
          file = File.new(entry, self)
          if @base.fileInclude == nil or @base.fileInclude.call(file)
            if @base.fileExclude == nil or not @base.fileExclude.call(file)
              @files << file
            end
          end
        end
      end
    end
    @dirs.sort_by! {|dir| dir.name}
    @files.sort_by! {|file| file.name}
  end
end
subDirs() click to toggle source

Get a list of all the sub-directories of this directory (with parents preceding children)

# File lib/based.rb, line 81
def subDirs
  result = []
  for dir in dirs
    result << dir
    result += dir.subDirs
  end
  return result
end