Class: Based::Directory

Inherits:
Object
  • Object
show all
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

BaseDirectory, SubDirectory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDirectory

initialise with un-initialised entries



33
34
35
# File 'lib/based.rb', line 33

def initialize
  @entries = nil
end

Instance Attribute Details

#baseObject (readonly)

The base directory (object)



15
16
17
# File 'lib/based.rb', line 15

def base
  @base
end

#fullPathObject (readonly)

The full path of the file



30
31
32
# File 'lib/based.rb', line 30

def fullPath
  @fullPath
end

#nameObject (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

#parentObject (readonly)

The parent directory (nil for the base directory)



27
28
29
# File 'lib/based.rb', line 27

def parent
  @parent
end

#pathElementsObject (readonly)

The elements of the relative path as an array



21
22
23
# File 'lib/based.rb', line 21

def pathElements
  @pathElements
end

#relativePathObject (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

#allFilesObject

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

#dirsObject

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

#filesObject

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

#getEntriesObject

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

#subDirsObject

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