Class: Dir

Inherits:
Object show all
Defined in:
lib/core/dir.rb

Overview

All methods here take an optional directory argument, defaulting to the current directory.

Class Method Summary collapse

Class Method Details

.dir_children(dirname = mpwd) ⇒ Object

Returns entries from simple_entries() that are directories.



9
10
11
12
13
# File 'lib/core/dir.rb', line 9

def self.dir_children(dirname=mpwd)
  self.simple_entries(dirname).find_all {|e|
    File.directory?(File.join(dirname, e))
  }
end

.file_children(dirname = mpwd) ⇒ Object

Returns entries from simple_entries() that are files.



16
17
18
19
20
# File 'lib/core/dir.rb', line 16

def self.file_children(dirname=mpwd)
  self.simple_entries(dirname).find_all {|e|
    File.file?(File.join(dirname,e))
  }
end

.full_entries(dirname = mpwd) ⇒ Object

Returns the full paths of simple_entries().



38
39
40
# File 'lib/core/dir.rb', line 38

def self.full_entries(dirname=mpwd)
  self.simple_entries(dirname).map {|e| File.join(dirname,e) }
end

.get_level_children(dirname, level) ⇒ Object

used recursively by levels_of_children



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/core/dir.rb', line 52

def self.get_level_children(dirname,level) #:nodoc:
  dir_children = self.full_entries(dirname)
  @level_children += dir_children
  if level < @max_level
    dir_children.each {|e|
      if File.directory?(e)
        self.get_level_children(e,level + 1)
      end
    }
  end
end

.levels_of_children(dirname = mpwd, max_level = 1000) ⇒ Object

Returns all simple_entries under a directory for the specified depth. If no depth specified it’ll return all entries under the directory.



44
45
46
47
48
49
# File 'lib/core/dir.rb', line 44

def self.levels_of_children(dirname=mpwd,max_level=1000)
  @max_level = max_level
  @level_children = []
  self.get_level_children(dirname,0)
  @level_children
end

.mpwdObject

Returns current directory with a ‘/’ appended.



4
5
6
# File 'lib/core/dir.rb', line 4

def self.mpwd
  Dir.pwd + "/"
end

Returns entries from simple_entries() that are not symlinks.



31
32
33
34
35
# File 'lib/core/dir.rb', line 31

def self.nonlink_entries(dirname=mpwd)
  self.simple_entries(dirname).select {|e|
    ! File.symlink?(File.join(dirname,e))
  }
end

.simple_entries(dirname = mpwd) ⇒ Object

Returns everything in a directory that entries() would except for ‘.’, ‘..’ and vim’s backup files ie files ending with ~ or .sw*. You should override this method to take advantage of methods based on it.



25
26
27
28
# File 'lib/core/dir.rb', line 25

def self.simple_entries(dirname=mpwd)
  dir_files = Dir.entries(dirname)
  files = dir_files - ['.','..'] - dir_files.grep(/~$/) - dir_files.grep(/\.sw[o-z]$/)
end