Trivet
Trivet is a generic class for organizing a hierarchy.
Basic usage
1. food = Trivet::Node.new('food')
2.
3. food.node('spices') do |spices|
4. spices.node 'paprika'
5.
6. spices.node('pepper') do |pepper|
7. pepper.node 'java'
8. pepper.node 'matico'
9. pepper.node 'cubeb'
10. end
11. end
12.
13. food.node('fruit') do |fruit|
14. fruit.node('red') do |red|
15. red.node 'cherry'
16. red.node 'apple'
17. end
18. end
19.
20. puts food.to_tree
Line 1 creates a Trivet::Node
object and assigns it the id "food".
Lines 3 to 18 use the node()
method to create child nodes.
Line 20 uses the to_tree()
method to display the tree as text, which
looks like this:
food
spices
paprika
pepper
java
matico
cubeb
fruit
red
cherry
apple
The tree can include non Trivet::Node objects.
food = Trivet::Node.new('food')
food.node('spices') do |spices|
spices.node('paprika') do |paprika|
paprika.children.push 'By from Fred'
end
end
See documentation for Trivet::Node
, Trivet::Document
, and Trivet::ChildSet
for more documentation.
Install
gem install codeblock
Name
The name "Trivet" doesn't have any particular significance. My friend has a cat named "Trivet". Meow.
Author
Mike O'Sullivan [email protected]
History
version | date | notes |
---|---|---|
1.0 | June 18, 2020 | Initial upload. |
1.1 | June 22, 2020 | Consolidated can_have_children? and can_have_child? into allow_child?. |
1.2 | January 3, 2021 | Added several methods to Trivet::Node: next_sibling, previous_sibling, have_child?, and move_child. |
1.3 | January 4, 2021 | Renamed have_child? to have_object?. Renamed remove_child() to remove_object(). |
1.4 | January 4, 2021 | Fixed documentation. No changes to code. |