Class: Based::Directory
- Inherits:
-
Object
- Object
- Based::Directory
- Defined in:
- lib/based.rb
Overview
A base class for directories: i.e. either the base directory itself, or a sub-directory
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
The base directory (object).
-
#fullPath ⇒ Object
readonly
The full path of the file.
-
#name ⇒ Object
readonly
The immediate name of the directory (nil for the base directory).
-
#parent ⇒ Object
readonly
The parent directory (nil for the base directory).
-
#pathElements ⇒ Object
readonly
The elements of the relative path as an array.
-
#relativePath ⇒ Object
readonly
The path of this directory relative to the base directory (includes a following “/” if non-empty).
Instance Method Summary collapse
-
#allFiles ⇒ Object
Get a list of all files contained within this directory.
-
#dirs ⇒ Object
Get list of directories immediately contained in this directory.
-
#files ⇒ Object
Get list of files immediately contained in this directory.
-
#getEntries ⇒ Object
get the “entries”, i.e.
-
#initialize ⇒ Directory
constructor
initialise with un-initialised entries.
-
#subDirs ⇒ Object
Get a list of all the sub-directories of this directory (with parents preceding children).
Constructor Details
#initialize ⇒ Directory
initialise with un-initialised entries
33 34 35 |
# File 'lib/based.rb', line 33 def initialize @entries = nil end |
Instance Attribute Details
#base ⇒ Object (readonly)
The base directory (object)
15 16 17 |
# File 'lib/based.rb', line 15 def base @base end |
#fullPath ⇒ Object (readonly)
The full path of the file
30 31 32 |
# File 'lib/based.rb', line 30 def fullPath @fullPath end |
#name ⇒ Object (readonly)
The immediate name of the directory (nil for the base directory)
24 25 26 |
# File 'lib/based.rb', line 24 def name @name end |
#parent ⇒ Object (readonly)
The parent directory (nil for the base directory)
27 28 29 |
# File 'lib/based.rb', line 27 def parent @parent end |
#pathElements ⇒ Object (readonly)
The elements of the relative path as an array
21 22 23 |
# File 'lib/based.rb', line 21 def pathElements @pathElements end |
#relativePath ⇒ Object (readonly)
The path of this directory relative to the base directory (includes a following “/” if non-empty)
18 19 20 |
# File 'lib/based.rb', line 18 def relativePath @relativePath end |
Instance Method Details
#allFiles ⇒ Object
Get a list of all files contained within this directory
91 92 93 94 95 96 97 |
# File 'lib/based.rb', line 91 def allFiles result = files for subDir in subDirs result += subDir.files end return result end |
#dirs ⇒ Object
Get list of directories immediately contained in this directory
75 76 77 78 |
# File 'lib/based.rb', line 75 def dirs getEntries() return @dirs end |
#files ⇒ Object
Get list of files immediately contained in this directory
69 70 71 72 |
# File 'lib/based.rb', line 69 def files getEntries() return @files end |
#getEntries ⇒ Object
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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 ⇒ Object
Get a list of all the sub-directories of this directory (with parents preceding children)
81 82 83 84 85 86 87 88 |
# File 'lib/based.rb', line 81 def subDirs result = [] for dir in dirs result << dir result += dir.subDirs end return result end |