wedding-planner/app/services/affinity_groups_hierarchy.rb

28 lines
639 B
Ruby
Raw Permalink Normal View History

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