wedding-planner/app/services/affinity_groups_hierarchy.rb
Manuel Bustillo 55e6cfcd36
Some checks failed
Check usage of free licenses / check-licenses (pull_request) Failing after 1m53s
Run unit tests / unit_tests (pull_request) Failing after 3m11s
Add copyright notice / copyright_notice (pull_request) Successful in 3m16s
Fix order of Ruby's magic string comment and Copyright assignment
2024-12-28 18:37:47 +01:00

48 lines
907 B
Ruby

# Copyright (C) 2024 Manuel Bustillo
# frozen_string_literal: true
class AffinityGroupsHierarchy < Array
include Singleton
def initialize
super
@references = {}
Group.roots.each do |group|
self << group.id
hydrate(group)
end
end
def find(id)
@references[id]
end
def <<(id)
new_node = Tree::TreeNode.new(id)
super(new_node).tap { @references[id] = new_node }
end
def register_child(parent_id, child_id)
@references[parent_id] << Tree::TreeNode.new(child_id).tap { |child_node| @references[child_id] = child_node }
end
def distance(id_a, id_b)
return nil if @references[id_a].nil? || @references[id_b].nil?
@references[id_a].distance_to_common_ancestor(@references[id_b])
end
private
def hydrate(group)
group.children.each do |child|
register_child(group.id, child.id)
hydrate(child)
end
end
end