Define an initializer with a hierarchy of affinity groups #16

Merged
bustikiller merged 1 commits from affinity-groups-hierarchy into main 2024-07-31 20:37:17 +00:00
3 changed files with 86 additions and 12 deletions

View File

@ -0,0 +1,27 @@
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

View File

@ -0,0 +1,39 @@
require_relative '../../app/services/affinity_groups_hierarchy'
hierarchy = AffinityGroupsHierarchy.instance
hierarchy << 'guests_a'
hierarchy << 'guests_b'
hierarchy << 'common_guests'
hierarchy.register_child('guests_a', 'family_a')
hierarchy.register_child('family_a', 'close_family_a')
hierarchy.register_child('family_a', 'cousins_a')
hierarchy.register_child('family_a', 'relatives_a')
hierarchy.register_child('guests_a', 'work_a')
hierarchy.register_child('work_a', 'besties_work_a')
hierarchy.register_child('guests_a', 'friends_a')
hierarchy.register_child('friends_a', 'college_friends_a')
hierarchy.register_child('friends_a', 'high_school_friends_a')
hierarchy.register_child('friends_a', 'childhood_friends_a')
hierarchy.register_child('guests_a', 'sports_a')
hierarchy.register_child('sports_a', 'basket_team_a')
hierarchy.register_child('sports_a', 'football_team_a')
hierarchy.register_child('guests_b', 'family_b')
hierarchy.register_child('family_b', 'close_family_b')
hierarchy.register_child('family_b', 'cousins_b')
hierarchy.register_child('family_b', 'relatives_b')
hierarchy.register_child('guests_b', 'work_b')
hierarchy.register_child('work_b', 'besties_work_b')
hierarchy.register_child('guests_b', 'friends_b')
hierarchy.register_child('friends_b', 'college_friends_b')
hierarchy.register_child('friends_b', 'high_school_friends_b')
hierarchy.register_child('friends_b', 'childhood_friends_b')
hierarchy.register_child('common_guests', 'dance_club')

View File

@ -28,19 +28,26 @@ Expense.create!(name: 'Transportation', amount: 3000, pricing_type: 'fixed')
Expense.create!(name: 'Invitations', amount: 200, pricing_type: 'fixed') Expense.create!(name: 'Invitations', amount: 200, pricing_type: 'fixed')
Expense.create!(name: 'Cake', amount: 500, pricing_type: 'fixed') Expense.create!(name: 'Cake', amount: 500, pricing_type: 'fixed')
samples = { samples = {
close_family: 10, close_family_a: 10,
family_1_group_a: 5, close_family_b: 10,
family_1_group_b: 5, cousins_a: 20,
family_2_group_a: 7, cousins_b: 15,
family_2_group_b: 15, relatives_a: 15,
previous_company: 5, relatives_b: 10,
new_company: 20, work_a: 10,
college: 10, work_b: 10,
high_school: 5, besties_work_a: 5,
childhood: 5, besties_work_b: 5,
basket_team: 20, college_friends_a: 10,
soccer_team: 15, college_friends_b: 10,
high_school_friends_a: 10,
high_school_friends_b: 10,
childhood_friends_a: 10,
childhood_friends_b: 10,
basket_team_a: 10,
football_team_a: 15,
dance_club: 10 dance_club: 10
}.each_with_object([]) do |(affinity_group, count), acc| }.each_with_object([]) do |(affinity_group, count), acc|
count.times { acc << affinity_group } count.times { acc << affinity_group }
@ -56,6 +63,7 @@ end
guest.save! guest.save!
end end
# Add unbreakable bonds
Guest.affinity_group_counts.each do |group| Guest.affinity_group_counts.each do |group|
couples = (group.taggings_count / 4).floor couples = (group.taggings_count / 4).floor