class Htmldu::Dirtree

Constants

BLOCK_SIZE

Attributes

blocks[R]
children[R]
name[R]
path[R]
root[R]

Public Class Methods

new(root, name = nil) click to toggle source
# File lib/htmldu/dirtree.rb, line 9
def initialize(root, name = nil)
  @root = root
  @name = name
  @path = name ? File.join(root, name) : root
end

Public Instance Methods

generate_tree() click to toggle source
# File lib/htmldu/dirtree.rb, line 15
def generate_tree()
  blocks = 0
  @children = []
  Dir.foreach(@path) do |name|
    next if name == '.' or name == '..'
    path = File.join(@path, name)
    stat = File.lstat(path)
    blocks += stat.blocks

    if stat.directory? and !stat.symlink?
      child = Dirtree.new(@path, name)
      child.generate_tree
      @children << child
      blocks += child.blocks
    end
  end
  @blocks = blocks
end
to_json(json: '') click to toggle source
# File lib/htmldu/dirtree.rb, line 34
def to_json(json: '')
  json << "{\"name\":\"#{@name}\","
  json << "\"path\":\"#{@path}\","
  json << "\"blocks\":#{@blocks},"
  json << '"children":['
  c = ''
  @children.each { |dir|
    json << c
    dir.to_json(json: json)
    c = ','
  }
  json << ']}'
end