Class: FileTreeProfiler::DirFile

Inherits:
File
  • Object
show all
Defined in:
lib/file_tree_profiler/dir_file.rb

Constant Summary collapse

EMPTY_CHECKSUM =
::Digest::MD5.hexdigest('/no-children/').freeze

Instance Attribute Summary collapse

Attributes inherited from File

#name, #parent

Instance Method Summary collapse

Methods inherited from File

#inspect, #path, #relative_path

Constructor Details

#initialize(*args) ⇒ DirFile

Returns a new instance of DirFile.



6
7
8
9
# File 'lib/file_tree_profiler/dir_file.rb', line 6

def initialize *args
  super *args
  walk
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/file_tree_profiler/dir_file.rb', line 4

def children
  @children
end

Instance Method Details

#checksumObject

checksum generated using concatenated checksums of all children



36
37
38
39
40
41
42
43
44
# File 'lib/file_tree_profiler/dir_file.rb', line 36

def checksum
  @checksum ||= begin
    if empty?
      EMPTY_CHECKSUM
    else
      ::Digest::MD5.hexdigest(children.map(&:checksum).join)
    end
  end.to_s
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  children.empty?
end

#sizeObject



31
32
33
# File 'lib/file_tree_profiler/dir_file.rb', line 31

def size
  children.inject(1) {|sum, c| sum += c.size; sum }
end

#walkObject

collects all children as DirFile or DataFile objects and is invoked on each collected DirFile object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/file_tree_profiler/dir_file.rb', line 13

def walk
  FileTreeProfiler.monitor_report(:profile, :dir, path)
  @children = []
  Dir.foreach(path) do |entry|
    next if (entry == '..' || entry == '.')
    full_path = ::File.join(path, entry)
    if ::File.directory?(full_path)
      children.push DirFile.new(self, entry)
    else
      children.push DataFile.new(self, entry)
    end
  end
end