Class: HierarchyWriter

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

Class Method Summary collapse

Class Method Details

.dump_ancestors_of(node) ⇒ Object

print the ancestors of node as a tree using line drawing characters node is expected to respond to #name and #ancestors returns the indent array which can be passed to dump_descendants_of



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/HierarchyWriter.rb', line 35

def self.dump_ancestors_of node

	indent = []
	ansc = node.ancestors

	ansc.each_with_index do |c, index| 
		indent.each_with_index do |draw, index| 
			if index > 0 
				print "  "
			end
		end 

		if index != 0
			print "└─"
		end
		puts "" + c.name
		last = index == indent.size - 1 
		indent.push !last
	end 

	indent
end

.dump_descendants_of(node, indent = [], depth = :depth_all) ⇒ Object

print the descendants of node as a tree using line drawing characters node is expected to respond to #name and #descendants



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/HierarchyWriter.rb', line 5

def self.dump_descendants_of node, indent = [], depth = :depth_all
	indent.each_with_index do |draw, index| 
		last = index == indent.size - 1 
		if draw 
			print last ? "" : " "
		else
			print last ? "" : ""
		end
		print last ? "" : " "
	end 

	puts "" + node.name

	if depth != :depth_none
		desc = node.descendants
		if desc
			if depth == :depth_immediate
				depth = :depth_none
			end
			desc.each_with_index do |c, index|
				last = index == desc.size - 1
				self.dump_descendants_of c, indent.clone.push(last), depth
			end
		end
	end
end

.dump_hierarchy_of(node, depth = :depth_all) ⇒ Object

prints the ancestors and descendants of node



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

def self.dump_hierarchy_of node, depth = :depth_all
	indent = HierarchyWriter::dump_ancestors_of node
	HierarchyWriter::dump_descendants_of node, indent, depth
end