Sha256: 9871c047db836ba1fd84e9f6694eaaea51b1aec316be91b03a27382ca285d1dc
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
require "tree_graph/version" module TreeGraph def tree_graph TopDown.new(self).tree_graph end def tree_graph_bottom_up BottomUp.new(self).tree_graph end def tree_graph_bottom_up_in_same_order BottomUpInSameOrder.new(self).tree_graph end EMPTY_ARRAY = [] class ::Object def label_for_tree_graph to_s end def children_for_tree_graph EMPTY_ARRAY end end module Node attr_accessor :is_last attr_reader :raw_node, :parent def initialize raw_node, parent=nil @raw_node, @parent, @is_last = raw_node, parent, false end def children_nodes children.map do |c| self.class.new(c, self) end.tap do |nodes| nodes.last.is_last = true unless nodes.empty? end end def children raw_node.children_for_tree_graph end def level [indent, branch, raw_node.label_for_tree_graph].join end def levels [level] + children_nodes.map(&:tree_graph) end def ancestors return EMPTY_ARRAY unless parent parent.ancestors + [parent] end def indent ancestors.map do |a| unless a.parent '' else a.is_last ? ' ' : '│ ' end end.join end end class TopDown include Node def tree_graph levels.join("\n") end def branch return '' unless parent is_last ? '└─' : '├─' end end class BottomUp include Node def tree_graph levels.reverse.join("\n") end def branch return '' unless parent is_last ? '┌─' : '├─' end end class BottomUpInSameOrder < BottomUp def children super.reverse end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tree_graph-0.2.4 | lib/tree_graph.rb |
tree_graph-0.2.3 | lib/tree_graph.rb |