wedding-planner/app/services/affinity_groups_hierarchy.rb
Manuel Bustillo 726470267d
All checks were successful
Run unit tests / unit_tests (pull_request) Successful in 2m7s
Define an initializer with a hierarchy of affinity groups
2024-07-31 20:58:47 +02:00

28 lines
639 B
Ruby

class AffinityGroupsHierarchy < Array
include Singleton
def initialize
super
@references = {}
end
def find(name)
@references[name]
end
def <<(name)
new_node = Tree::TreeNode.new(name)
super(new_node).tap { @references[name] = new_node }
end
def register_child(parent_name, child_name)
@references[parent_name] << Tree::TreeNode.new(child_name).tap { |child_node| @references[child_name] = child_node }
end
def distance(name_a, name_b)
return nil if @references[name_a].nil? || @references[name_b].nil?
@references[name_a].distance_to_common_ancestor(@references[name_b])
end
end