Transform flat list into (reverse) dependency tree.
gem install Flat2Tree
Initialize + enter data for examples:
require 'flat2tree'
list = {
"Tom"=>["Kate", "John"],
"Kate"=>["John"],
"John"=>[]
}.map{|name, list| Flat2Tree::Entry.new(name, list)}
.to_s
produces tree like output compatible with YAML.
puts Flat2Tree.dependencies_from(list, 'Kate')
Kate:
- John
When no starting node name is given an nameless root container is created, use .nodes
to get array.
puts Flat2Tree.dependencies_from(list).nodes
Tom:
- Kate:
- John
- John
Kate:
- John
John
It is also to reverse the dependency tree so last leafs end up as roots
puts Flat2Tree.reverse_dependencies_from(list).simplify.nodes
John:
- Tom
- Kate:
- Tom
- handle missing node
- Michal Papis [email protected]