Class: Lina::Tree
- Inherits:
-
Object
- Object
- Lina::Tree
- Defined in:
- lib/lina/tree.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #_to_tree(root) ⇒ Object
-
#initialize(routes) ⇒ Tree
constructor
A new instance of Tree.
- #paths_insert_tree(paths, tree, route) ⇒ Object
- #paths_to_tree ⇒ Object
- #size ⇒ Object
- #to_tree ⇒ Object
Constructor Details
#initialize(routes) ⇒ Tree
Returns a new instance of Tree.
4 5 6 7 8 |
# File 'lib/lina/tree.rb', line 4 def initialize(routes) @routes = routes @size = 0 @root = paths_to_tree end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
3 4 5 |
# File 'lib/lina/tree.rb', line 3 def root @root end |
Class Method Details
.parse(routes) ⇒ Object
28 29 30 |
# File 'lib/lina/tree.rb', line 28 def self.parse(routes) self.new(routes).to_tree end |
Instance Method Details
#_to_tree(root) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/lina/tree.rb', line 18 def _to_tree(root) ret = {} ret[:label] = root.path ret[:values] = root.values if ! root.children.empty? ret[:children] = root.children.collect { |child| _to_tree(child) } end ret end |
#paths_insert_tree(paths, tree, route) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/lina/tree.rb', line 46 def paths_insert_tree(paths, tree, route) if paths.empty? @size += 1 tree.add_value(route) return end paths = paths.dup path = paths.shift path = path.gsub('(.:format)', '') endpoint = tree.find_child(path) if endpoint paths_insert_tree(paths, endpoint, route) else child = TreeNode.new(path, tree) tree.add_child(child) paths_insert_tree(paths, child, route) end end |
#paths_to_tree ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/lina/tree.rb', line 36 def paths_to_tree tree = TreeNode.new('/') @routes.each do |route| paths = route[:path].split("/") paths.shift paths_insert_tree(paths, tree, route) end tree end |
#size ⇒ Object
32 33 34 |
# File 'lib/lina/tree.rb', line 32 def size @size end |
#to_tree ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/lina/tree.rb', line 10 def to_tree ret = [] @root.children.each do |child| ret << _to_tree(child) end ret end |