Manuel Bustillo
e20a366410
All checks were successful
Check usage of free licenses / check-licenses (pull_request) Successful in 56s
Add copyright notice / copyright_notice (pull_request) Successful in 1m36s
Run unit tests / unit_tests (pull_request) Successful in 3m56s
Build Nginx-based docker image / build-static-assets (pull_request) Successful in 28m26s
48 lines
929 B
Ruby
48 lines
929 B
Ruby
# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
|
|
|
|
# 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
|