Class: DirectoryNode

Inherits:
Object
  • Object
show all
Defined in:
lib/directory_node.rb

Overview

Copyright © 2009 Matteo Sasso

This file is part of durb.

durb is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

durb is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with durb. If not, see <www.gnu.org/licenses/>.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, size, files, flags) ⇒ DirectoryNode

Returns a new instance of DirectoryNode.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/directory_node.rb', line 25

def initialize(path, size, files, flags)
  @path = path
  @size = size
  @files = files
  @s_subsize = @s_subfiles = @s_dirs = 0
  @i_subsize = @i_subfiles = @i_dirs = @i_rec_dirs = 0
  @big = true
  @show = false
  @subdirectories = []
  @flags = flags
end

Instance Attribute Details

#bigObject

Returns the value of attribute big.



20
21
22
# File 'lib/directory_node.rb', line 20

def big
  @big
end

#filesObject

Returns the value of attribute files.



20
21
22
# File 'lib/directory_node.rb', line 20

def files
  @files
end

#flagsObject

Returns the value of attribute flags.



23
24
25
# File 'lib/directory_node.rb', line 23

def flags
  @flags
end

#i_dirsObject

Returns the value of attribute i_dirs.



22
23
24
# File 'lib/directory_node.rb', line 22

def i_dirs
  @i_dirs
end

#i_rec_dirsObject

Returns the value of attribute i_rec_dirs.



22
23
24
# File 'lib/directory_node.rb', line 22

def i_rec_dirs
  @i_rec_dirs
end

#i_subfilesObject

Returns the value of attribute i_subfiles.



22
23
24
# File 'lib/directory_node.rb', line 22

def i_subfiles
  @i_subfiles
end

#i_subsizeObject

Returns the value of attribute i_subsize.



22
23
24
# File 'lib/directory_node.rb', line 22

def i_subsize
  @i_subsize
end

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/directory_node.rb', line 19

def path
  @path
end

#s_dirsObject

Returns the value of attribute s_dirs.



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

def s_dirs
  @s_dirs
end

#s_subfilesObject

Returns the value of attribute s_subfiles.



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

def s_subfiles
  @s_subfiles
end

#s_subsizeObject

Returns the value of attribute s_subsize.



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

def s_subsize
  @s_subsize
end

#showObject

Returns the value of attribute show.



20
21
22
# File 'lib/directory_node.rb', line 20

def show
  @show
end

#sizeObject

Returns the value of attribute size.



20
21
22
# File 'lib/directory_node.rb', line 20

def size
  @size
end

#subdirectoriesObject (readonly)

Returns the value of attribute subdirectories.



19
20
21
# File 'lib/directory_node.rb', line 19

def subdirectories
  @subdirectories
end

Class Method Details

.path_to_string(path) ⇒ Object



71
72
73
# File 'lib/directory_node.rb', line 71

def self.path_to_string(path)
  path.length == 0 ? '/' : ('/' + path.join('/'))
end

.string_to_path(path_string) ⇒ Object



75
76
77
# File 'lib/directory_node.rb', line 75

def self.string_to_path(path_string)
  path_string == '/' ? [] : path_string.split('/').reject{|component| component.empty?}
end

Instance Method Details

#add_subdirectory(d) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/directory_node.rb', line 41

def add_subdirectory(d)
  @subdirectories << d
  @s_subsize += d.s_subsize
  @s_subfiles += d.s_subfiles
  @s_dirs += d.s_dirs
  @flags << :imprecise if not d.precise? and precise?
  if d.big
    @s_subsize += d.size + d.i_subsize
    @s_subfiles += d.files + d.i_subfiles
    @s_dirs += 1 + d.i_dirs
  else
    @i_subsize += d.size + d.i_subsize
    @i_subfiles += d.files + d.i_subfiles
    @i_rec_dirs += 1 + d.i_dirs
    @i_dirs += 1
  end
end

#inspect(indentation = 0) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/directory_node.rb', line 79

def inspect(indentation = 0)
  ' ' * indentation +
        "#<DirectoryNode:#{path_string} size=#{@size} " +
        (@big ? "big " : "small ") + (@show ? "shown" : "hidden") +
        (@subdirectories.empty? ? "" : ("\n" + @subdirectories.map{|d| d.inspect(indentation+2)}.join("\n"))) +
        ">"
end

#nameObject



67
68
69
# File 'lib/directory_node.rb', line 67

def name
  @path[-1]
end

#path_stringObject



63
64
65
# File 'lib/directory_node.rb', line 63

def path_string
  self.class.path_to_string(@path)
end

#precise?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/directory_node.rb', line 37

def precise?
  @flags.include?(:imprecise) ? false : true
end

#shown_subdirsObject



59
60
61
# File 'lib/directory_node.rb', line 59

def shown_subdirs
  @subdirectories.select{|n| n.show}
end