class Based::Directory
A base class for directories: i.e. either the base directory itself, or a sub-directory
Attributes
The base directory (object)
The full path of the file
The immediate name of the directory (nil for the base directory)
The parent directory (nil for the base directory)
The elements of the relative path as an array
The path of this directory relative to the base directory (includes a following “/” if non-empty)
Public Class Methods
initialise with un-initialised entries
# File lib/based.rb, line 33 def initialize @entries = nil end
Public Instance Methods
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
Get list of directories immediately contained in this directory
# File lib/based.rb, line 75 def dirs getEntries() return @dirs end
Get list of files immediately contained in this directory
# File lib/based.rb, line 69 def files getEntries() return @files end
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
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