Class: Utilities::Hierarchy
- Inherits:
-
Object
- Object
- Utilities::Hierarchy
- Defined in:
- lib/utilities/hierarchy.rb
Overview
Facilitate sorting and rendering objects that have an id and parent_id.
Instance Attribute Summary collapse
-
#hierarchy ⇒ Object
Hash {id => [child ids], id2 => [] } Not really a hierarchy, just a parent/child lookup.
-
#match ⇒ Object
permits return a simple true/false property in the result.
-
#node_map ⇒ Object
Hash { node_id => object, … }.
-
#nodes ⇒ Object
Array of objects with interface of properties above.
-
#observed ⇒ Object
Array memoized list of node ids.
-
#root ⇒ Object
Array asserted root ids.
-
#root_nodes ⇒ Object
Array inferred root ids, not used when ‘root` provided.
Instance Method Summary collapse
- #build(node_id, level = 0, result = []) ⇒ Object
- #build_hierarchy ⇒ Object
- #draw(node_id, level = 0, result = "") ⇒ Object
-
#initialize(params = {}) ⇒ Hierarchy
constructor
A new instance of Hierarchy.
- #match? ⇒ Boolean
- #matched?(node_id) ⇒ Boolean
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Hierarchy
Returns a new instance of Hierarchy.
44 45 46 47 48 49 |
# File 'lib/utilities/hierarchy.rb', line 44 def initialize( params = {} ) @root = params[:root_id] @nodes = params[:objects] @match = params[:match] || [] @node_map = nodes.inject({}) {|hsh, n| hsh[n.id] = n; hsh} end |
Instance Attribute Details
#hierarchy ⇒ Object
Returns Hash {id => [child ids], id2 => [] } Not really a hierarchy, just a parent/child lookup.
25 26 27 |
# File 'lib/utilities/hierarchy.rb', line 25 def hierarchy @hierarchy end |
#match ⇒ Object
permits return a simple true/false property in the result
42 43 44 |
# File 'lib/utilities/hierarchy.rb', line 42 def match @match end |
#node_map ⇒ Object
Returns Hash { node_id => object, … }.
20 21 22 |
# File 'lib/utilities/hierarchy.rb', line 20 def node_map @node_map end |
#nodes ⇒ Object
Returns Array of objects with interface of properties above.
16 17 18 |
# File 'lib/utilities/hierarchy.rb', line 16 def nodes @nodes end |
#observed ⇒ Object
Returns Array memoized list of node ids.
37 38 39 |
# File 'lib/utilities/hierarchy.rb', line 37 def observed @observed end |
#root ⇒ Object
Returns Array asserted root ids.
33 34 35 |
# File 'lib/utilities/hierarchy.rb', line 33 def root @root end |
#root_nodes ⇒ Object
Returns Array inferred root ids, not used when ‘root` provided.
29 30 31 |
# File 'lib/utilities/hierarchy.rb', line 29 def root_nodes @root_nodes end |
Instance Method Details
#build(node_id, level = 0, result = []) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/utilities/hierarchy.rb', line 103 def build(node_id, level = 0, result = []) a = [node_id, node_map[node_id].label, (node_map[node_id].respond_to?(:alias) ? node_map[node_id].alias : nil), level] a.push matched?(node_id) if match? result.push a hierarchy[node_id]&.each do |child| result += build(child, level + 1) end result end |
#build_hierarchy ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/utilities/hierarchy.rb', line 55 def build_hierarchy @hierarchy = {} nodes.each do |n| @hierarchy[n.id] = [] end nodes.each do |n| if parent_id = node_map[n.id]&.parent_id @hierarchy[parent_id]&.push(n.id) end end @hierarchy end |
#draw(node_id, level = 0, result = "") ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/utilities/hierarchy.rb', line 81 def draw(node_id, level = 0, result = []) z = ' ' * level + "ID: #{node_id} #{node_map[node_id].label}\n" # puts z result.push z # += z hierarchy[node_id]&.each do |child| result += draw(child, level + 1) end result end |
#match? ⇒ Boolean
129 130 131 |
# File 'lib/utilities/hierarchy.rb', line 129 def match? !match.empty? end |
#matched?(node_id) ⇒ Boolean
133 134 135 |
# File 'lib/utilities/hierarchy.rb', line 133 def matched?(node_id) match.include?(node_id) end |
#to_a ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/utilities/hierarchy.rb', line 113 def to_a a = [] root_nodes.each do |id| a += build(id, 0) end a end |
#to_s ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/utilities/hierarchy.rb', line 121 def to_s s = '' root_nodes.each do |id| s += draw(id, 0) end s end |