View Source CTE.Utils (Closure Table v2.0.5)
Basic utilities for helping developing functionality about the CTE
data structures
Summary
Functions
render a path hierarchy as a .dot string that you could use for drawing your results, using graphviz. w⦿‿⦿t!
print the tree at the console, using a custom function for selecting the info to be displayed
converts the given tree into a nested map
Functions
render a path hierarchy as a .dot string that you could use for drawing your results, using graphviz. w⦿‿⦿t!
Upon receiving something like this:
%{
nodes: %{
6 => %{
author: "Rolie",
comment: "Everything is easier, than with the Nested Sets.",
id: 6
},
8 => %{
author: "Olie",
comment: "I’m sold! And I’ll use its Elixir implementation! <3",
id: 8
},
9 => %{author: "Polie", comment: "w⦿‿⦿t!", id: 9}
},
paths: [
[6, 6, 0],
[6, 8, 1],
[8, 8, 0],
[6, 9, 1],
[9, 9, 0]
]
}
this will output a .dot formatted string that you could use later for generating
an image: dot -Tpng <filename>.dot -o <filename>.png
print the tree at the console, using a custom function for selecting the info to be displayed
The print_tree/3 function receives the tree structure returned by the CTE, the id of an existing node we want to start printing the tree with, followed by the options.
Options
:callback
- a function that it is invoked for every node in the tree. Has two parameters:- id - the id of the node we render, ao a node specific unique identifier i.e. node name, etc.
- nodes - the nodes received the tree structure, a map %{node_id => node}...any()
This function must return a tuple with two elements. First element is the name of the node we render, the second one being any optional info you want to add.
:raw
- return a list of tuples if true. Each tuple will contain the depth of the text returned from the callback. Useful for custom formatting the output of the print.
Example, using the default options:
iex» {:ok, tree} = CTT.tree(1) iex» CTE.Utils.print_tree(tree,1, callback: &({&2[&1].author <> ":", &2[&1].comment}))
Olie: Is Closure Table better than the Nested Sets? └── Rolie: It depends. Do you need referential integrity? └── Olie: Yeah.
converts the given tree into a nested map
This function receives a tree structure returned by the CTE and the id of an existing node we want to start from. It returns a nested map representation of the tree.
Options
:callback
- a function of arity one that accepts the current node as input and outputs a formatted node. Defaults to& &1
.
Example:
iex» {:ok, tree} = CTT.tree(1) iex» CTE.Utils.tree_to_map(tree, 6, callback: &Map.take(&1, [:text]))
%{ "children" => [
%{
"children" => [],
"id" => 8,
"node" => %{text: "I’m sold! And I’ll use its Elixir implementation! <3"}
},
%{"children" => [], "id" => 9, "node" => %{text: "w⦿‿⦿t!"}}
], "id" => 6, "node" => %{text: "Everything is easier, than with the Nested Sets."} }