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.
Constant Summary collapse
- Node =
Data.define(:id, :parent_id, :label, :url, :weight, :klass, :alias)
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
def draw(node_id, level = 0, result = []) z = ‘ ’ * level + “ID: #node_id #.labeln” # puts z result.push z # = z hierarchy&.each do |child| result = draw(child, level + 1) end result end.
-
#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.
46 47 48 49 50 51 |
# File 'lib/utilities/hierarchy.rb', line 46 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.
27 28 29 |
# File 'lib/utilities/hierarchy.rb', line 27 def hierarchy @hierarchy end |
#match ⇒ Object
permits return a simple true/false property in the result
44 45 46 |
# File 'lib/utilities/hierarchy.rb', line 44 def match @match end |
#node_map ⇒ Object
Returns Hash { node_id => object, … }.
22 23 24 |
# File 'lib/utilities/hierarchy.rb', line 22 def node_map @node_map end |
#nodes ⇒ Object
Returns Array of objects with interface of properties above.
18 19 20 |
# File 'lib/utilities/hierarchy.rb', line 18 def nodes @nodes end |
#observed ⇒ Object
Returns Array memoized list of node ids.
39 40 41 |
# File 'lib/utilities/hierarchy.rb', line 39 def observed @observed end |
#root ⇒ Object
Returns Array asserted root ids.
35 36 37 |
# File 'lib/utilities/hierarchy.rb', line 35 def root @root end |
#root_nodes ⇒ Object
Returns Array inferred root ids, not used when ‘root` provided.
31 32 33 |
# File 'lib/utilities/hierarchy.rb', line 31 def root_nodes @root_nodes end |
Instance Method Details
#build(node_id, level = 0, result = []) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/utilities/hierarchy.rb', line 105 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
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/utilities/hierarchy.rb', line 57 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
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
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/utilities/hierarchy.rb', line 93 def draw(node_id, level = 0, result = "") z = ' ' * level + node_map[node_id].label z << '*' if match? && matched?(node_id) z << "\n" result += z hierarchy[node_id]&.each do |child| result += draw(child, level + 1) end result end |
#match? ⇒ Boolean
131 132 133 |
# File 'lib/utilities/hierarchy.rb', line 131 def match? !match.empty? end |
#matched?(node_id) ⇒ Boolean
135 136 137 |
# File 'lib/utilities/hierarchy.rb', line 135 def matched?(node_id) match.include?(node_id) end |
#to_a ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/utilities/hierarchy.rb', line 115 def to_a a = [] root_nodes.each do |id| a += build(id, 0) end a end |
#to_s ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/utilities/hierarchy.rb', line 123 def to_s s = '' root_nodes.each do |id| s += draw(id, 0) end s end |