From 726470267d75521021e7e219df6a2cee085147ca Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Wed, 31 Jul 2024 20:58:47 +0200 Subject: [PATCH] Define an initializer with a hierarchy of affinity groups --- app/services/affinity_groups_hierarchy.rb | 27 ++++++++++++++++ config/initializers/affinity_groups.rb | 39 +++++++++++++++++++++++ db/seeds.rb | 32 ++++++++++++------- 3 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 app/services/affinity_groups_hierarchy.rb create mode 100644 config/initializers/affinity_groups.rb diff --git a/app/services/affinity_groups_hierarchy.rb b/app/services/affinity_groups_hierarchy.rb new file mode 100644 index 0000000..af6c62a --- /dev/null +++ b/app/services/affinity_groups_hierarchy.rb @@ -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 diff --git a/config/initializers/affinity_groups.rb b/config/initializers/affinity_groups.rb new file mode 100644 index 0000000..f52c1d4 --- /dev/null +++ b/config/initializers/affinity_groups.rb @@ -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') \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index 12a5fa8..3f26f4c 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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: 'Cake', amount: 500, pricing_type: 'fixed') + samples = { - close_family: 10, - family_1_group_a: 5, - family_1_group_b: 5, - family_2_group_a: 7, - family_2_group_b: 15, - previous_company: 5, - new_company: 20, - college: 10, - high_school: 5, - childhood: 5, - basket_team: 20, - soccer_team: 15, + close_family_a: 10, + close_family_b: 10, + cousins_a: 20, + cousins_b: 15, + relatives_a: 15, + relatives_b: 10, + work_a: 10, + work_b: 10, + besties_work_a: 5, + besties_work_b: 5, + college_friends_a: 10, + 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 }.each_with_object([]) do |(affinity_group, count), acc| count.times { acc << affinity_group } @@ -56,6 +63,7 @@ end guest.save! end +# Add unbreakable bonds Guest.affinity_group_counts.each do |group| couples = (group.taggings_count / 4).floor -- 2.47.1