Module: Eco::API::Common::People::SupervisorHelpers::ClassMethods
- Included in:
- Eco::API::Common::People::SupervisorHelpers
- Defined in:
- lib/eco/api/common/people/supervisor_helpers.rb
Instance Method Summary collapse
-
#identify_cyclic_chains(values) ⇒ Array<Array>
Identifies all the cyclic supervisor chains.
- #print_tree(tree, lev: 0) ⇒ Object
-
#sort_by_supervisors(values, supervisors_first: true) ⇒ Array<PersonEntry>
Reorders as follows: 1.
-
#supervisors_tree(values) ⇒ Hash
Generates a
Hash
tree structure, where: * keys are nodes * values areHash
subtree structures ofkey
subordinates. - #tree_to_str(tree, lev: 0) ⇒ Object
Instance Method Details
#identify_cyclic_chains(values) ⇒ Array<Array>
Note:
as supervisors_tree
will have any entry involved in a cycle at the top,
it just checks all the top entries against their offspring
Identifies all the cyclic supervisor chains
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/eco/api/common/people/supervisor_helpers.rb', line 41 def identify_cyclic_chains(values) raise "Expected non hash Enumerable. Given: #{values.class}" if values.is_a?(Hash) return [] unless values.is_a?(Enumerable) identify = proc do |top_sup, offspring, chain = [top_sup]| next [] if offspring.empty? offspring.each_with_object([]) do |(sup, subordinates), set| break set unless set.empty? if top_sup.supervisor_id == sup.id set.concat(chain, [sup]) else set = identify.call(top_sup, subordinates, chain | [sup]) end end end supervisors_tree(values).each_with_object([]) do |(top_sup, offspring), sets| if (set = identify.call(top_sup, offspring)) && !set.empty? sets.push(set) end end end |
#print_tree(tree, lev: 0) ⇒ Object
74 75 76 |
# File 'lib/eco/api/common/people/supervisor_helpers.rb', line 74 def print_tree(tree, lev: 0) puts tree_to_str(tree) end |
#sort_by_supervisors(values, supervisors_first: true) ⇒ Array<PersonEntry>
Reorders as follows:
- supervisors, people with no supervisor or where their supervisor not present
- subordinates
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eco/api/common/people/supervisor_helpers.rb', line 19 def sort_by_supervisors(values, supervisors_first: true) raise "Expected non hash Enumerable. Given: #{values.class}" if values.is_a?(Hash) return [] unless values.is_a?(Enumerable) roam = proc do |tree| [].tap do |out| sub_outs = tree.empty?? [] : tree.map {|sup, subtree| roam.call(subtree)} tree.each do |sup, subtree| sout = subtree.empty?? [] : roam.call(subtree) supervisors_first ? sout.unshift(sup) : sout.push(sup) out.concat(sout) end end end roam.call(supervisors_tree(values)) end |
#supervisors_tree(values) ⇒ Hash
Note:
it is resilient to cyclic supervisors (it will just add the last at the top)
Generates a Hash
tree structure, where:
- keys are nodes
- values are
Hash
subtree structures ofkey
subordinates
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/eco/api/common/people/supervisor_helpers.rb', line 85 def supervisors_tree(values) # rubocop:disable Metrics/AbcSize raise "Expected non hash Enumerable. Given: #{values.class}" if values.is_a?(Hash) return {} unless values.is_a?(Enumerable) idx = get_super_indexes(values) processed = [] subtree = proc do |entry, level, toptree| if processed.include?(entry) next {} unless toptree.key?(entry) && level.positive? # needs to be moved as a child subnodes = toptree.delete(entry) processed.delete(entry) end subnodes ||= {}.tap do |tree| subs = idx[:subordinates].call(entry) processed.push(entry) next nil unless subs && !subs.empty? subs.each do |sub| sub_tree = subtree.call(sub, level + 1, toptree) tree.merge!(sub_tree) end end {entry => subnodes} end {}.tap do |tree| idx[:by_sup].keys.each do |sup_id| if (sup = idx[:supers][sup_id]) tree.merge!(subtree.call(sup, 0, tree)) else idx[:by_sup][sup_id].each do |sub| tree.merge!(subtree.call(sub, 0, tree)) end end end end end |
#tree_to_str(tree, lev: 0) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/eco/api/common/people/supervisor_helpers.rb', line 64 def tree_to_str(tree, lev: 0) raise "Required Hash tree structure. Given: #{tree.class}" unless tree.is_a?(Hash) "".tap do |str| tree.each do |entry, subtree| str << "#{" " * lev}+-- #{entry.id || entry.external_id}\n" str << tree_to_str(subtree, lev: lev + 1) unless !subtree || subtree.empty? end end end |