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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#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



25
26
27
# File 'lib/utilities/hierarchy.rb', line 25

def hierarchy
  @hierarchy
end

#matchObject

permits return a simple true/false property in the result

Returns:

  • Array node_ids to be flagged as matched.



42
43
44
# File 'lib/utilities/hierarchy.rb', line 42

def match
  @match
end

#node_mapObject

Returns Hash { node_id => object, … }.

Returns:

  • Hash { node_id => object, … }



20
21
22
# File 'lib/utilities/hierarchy.rb', line 20

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



16
17
18
# File 'lib/utilities/hierarchy.rb', line 16

def nodes
  @nodes
end

#observedObject

Returns Array memoized list of node ids.

Returns:

  • Array memoized list of node ids



37
38
39
# File 'lib/utilities/hierarchy.rb', line 37

def observed
  @observed
end

#rootObject

Returns Array asserted root ids.

Returns:

  • Array asserted root ids



33
34
35
# File 'lib/utilities/hierarchy.rb', line 33

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



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_hierarchyObject



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

Returns:

  • (Boolean)


129
130
131
# File 'lib/utilities/hierarchy.rb', line 129

def match?
  !match.empty?
end

#matched?(node_id) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/utilities/hierarchy.rb', line 133

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

#to_aObject



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_sObject



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