Class: Ld::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/ld.rb,
lib/ld/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ Tree

Returns a new instance of Tree.



4
5
6
7
8
9
10
11
# File 'lib/ld/tree.rb', line 4

def initialize root_path
  @root_path = root_path
  @root_name = File.basename root_path
  @nodes = Ld::Nodes.new
  @numbers = 0
  @depth = 0
  @id = 0
end

Instance Attribute Details

#depthObject

Returns the value of attribute depth.



2
3
4
# File 'lib/ld/tree.rb', line 2

def depth
  @depth
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/ld/tree.rb', line 2

def id
  @id
end

#nodesObject

Returns the value of attribute nodes.



2
3
4
# File 'lib/ld/tree.rb', line 2

def nodes
  @nodes
end

#root_nameObject

Returns the value of attribute root_name.



2
3
4
# File 'lib/ld/tree.rb', line 2

def root_name
  @root_name
end

#root_pathObject

Returns the value of attribute root_path.



2
3
4
# File 'lib/ld/tree.rb', line 2

def root_path
  @root_path
end

Instance Method Details



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

def print_tree
  puts "#{@root_name}:"
  @nodes.sort_by_depth.each do |node|
    if node.type == 1
      print_tree
    else
      puts "\t"*node.id + "#{node.type}:#{node.name}"
    end
  end
end

#read_tree(path = @root_path) ⇒ Object



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

def read_tree path = @root_path
  @depth+=1
  Dir.foreach(path)do |p|
    if !p.match(/^\./)
      node = Ld::Node.new(@id+=1, @depth, "#{path}/#{p}")
      @nodes << node
      if node.type == 1
        read_tree node.path
      end
    end
  end
end