From 2ae1a8a2b289f010b31bcc34b54d7c7b51a2afb8 Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 25 Oct 2024 01:26:20 +1100 Subject: Refactor uncle and aunt relationship handling to support both paternal and maternal sides. --- lib/family_tree.rb | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/family_tree.rb b/lib/family_tree.rb index ec72d8e..0c9ecef 100644 --- a/lib/family_tree.rb +++ b/lib/family_tree.rb @@ -95,10 +95,10 @@ class FamilyTree end end - def handle_uncle_relationship(child_of_family, type) + def handle_sibling_relationship(child_of_family, type, side) return 'NONE' if child_of_family.nil? - parent = type == 'paternal' ? child_of_family.father : child_of_family.mother + parent = side == 'paternal' ? child_of_family.father : child_of_family.mother return 'NONE' if parent.nil? || parent.is_a?(NilPerson) # Find the family where the parent is a child @@ -110,29 +110,25 @@ class FamilyTree # Find the siblings of the parent siblings = find_siblings(parent_family, parent.name) - # Select male siblings (uncles) - uncles = siblings.select { |sibling| sibling.gender == Gender::MALE } - uncles.empty? ? 'NONE' : uncles.map(&:name).join(' ') + # Select based on sibling gender + relatives = case type + when 'uncle' + siblings.select { |sibling| sibling.gender == Gender::MALE } + when 'aunt' + siblings.select { |sibling| sibling.gender == Gender::FEMALE } + else + [] + end + + relatives.empty? ? 'NONE' : relatives.map(&:name).join(' ') end - def handle_aunt_relationship(child_of_family, type) - return 'NONE' if child_of_family.nil? - - parent = type == 'paternal' ? child_of_family.father : child_of_family.mother - return 'NONE' if parent.nil? || parent.is_a?(NilPerson) - - # Find the family where the parent is a child - parent_result = find_person_in_families(parent.name) - parent_family = parent_result[:child_of_family] - - return 'NONE' if parent_family.nil? - - # Find the siblings of the parent - siblings = find_siblings(parent_family, parent.name) + def handle_uncle_relationship(child_of_family, type) + handle_sibling_relationship(child_of_family, 'uncle', type) + end - # Select female siblings (aunts) - aunts = siblings.select { |sibling| sibling.gender == Gender::FEMALE } - aunts.empty? ? 'NONE' : aunts.map(&:name).join(' ') + def handle_aunt_relationship(child_of_family, type) + handle_sibling_relationship(child_of_family, 'aunt', type) end def handle_sister_in_law_relationship(person, child_of_family) -- cgit v1.2.3