Class: Utilities::Hierarchy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#hierarchyObject

Returns Hash {id => [child ids], id2 => [] } Not really a hierarchy, just a parent/child lookup.

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

#matchObject

permits return a simple true/false property in the result

Returns:

  • Array node_ids to be flagged as matched.



44
45
46
# File 'lib/utilities/hierarchy.rb', line 44

def match
  @match
end

#node_mapObject

Returns Hash { node_id => object, … }.

Returns:

  • Hash { node_id => object, … }



22
23
24
# File 'lib/utilities/hierarchy.rb', line 22

def node_map
  @node_map
end

#nodesObject

Returns Array of objects with interface of properties above.

Returns:

  • Array of objects with interface of properties above



18
19
20
# File 'lib/utilities/hierarchy.rb', line 18

def nodes
  @nodes
end

#observedObject

Returns Array memoized list of node ids.

Returns:

  • Array memoized list of node ids



39
40
41
# File 'lib/utilities/hierarchy.rb', line 39

def observed
  @observed
end

#rootObject

Returns Array asserted root ids.

Returns:

  • Array asserted root ids



35
36
37
# File 'lib/utilities/hierarchy.rb', line 35

def root
  @root
end

#root_nodesObject

Returns Array inferred root ids, not used when ‘root` provided.

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_hierarchyObject



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

Returns:

  • (Boolean)


131
132
133
# File 'lib/utilities/hierarchy.rb', line 131

def match?
  !match.empty?
end

#matched?(node_id) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/utilities/hierarchy.rb', line 135

def matched?(node_id)
  match.include?(node_id)
end

#to_aObject



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_sObject



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