diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-10-25 01:22:19 +1100 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-10-25 01:22:19 +1100 |
| commit | cb011a691c32690e6d925fe960bed989cdac5aeb (patch) | |
| tree | 02cd54c3b52810f7c8cd032ebeefbaf99c571510 | |
| parent | fa50df797d8fcb1e3636683cf2ff1582f35bc181 (diff) | |
Refactor sibling-in-law relationship methods to reduce redundancy
| -rw-r--r-- | lib/family_tree.rb | 43 |
1 files changed, 12 insertions, 31 deletions
diff --git a/lib/family_tree.rb b/lib/family_tree.rb index 09f367f..ec72d8e 100644 --- a/lib/family_tree.rb +++ b/lib/family_tree.rb @@ -136,57 +136,38 @@ class FamilyTree end def handle_sister_in_law_relationship(person, child_of_family) - sisters_in_law = [] - - # Check for female siblings of the spouse - if person.spouse.is_a?(Person) - spouse_result = find_person_in_families(person.spouse.name) - spouse_family = spouse_result[:child_of_family] - - if spouse_family - spouse_siblings = find_siblings(spouse_family, person.spouse.name) - sisters_in_law.concat(spouse_siblings.select { |sibling| sibling.gender == Gender::FEMALE }.map(&:name)) - end - end - - unless child_of_family.nil? - # Check for female spouses of siblings of the person - siblings = find_siblings(child_of_family, person.name) - - siblings.each do |sibling| - sisters_in_law << sibling.spouse.name if sibling.spouse && sibling.spouse.gender == Gender::FEMALE - end - end - - sisters_in_law.uniq! - sisters_in_law.empty? ? 'NONE' : sisters_in_law.join(' ') + find_in_laws(person, child_of_family, Gender::FEMALE) end def handle_brother_in_law_relationship(person, child_of_family) - brothers_in_law = [] + find_in_laws(person, child_of_family, Gender::MALE) + end + + def find_in_laws(person, child_of_family, gender) + in_laws = [] - # Check for male siblings of the spouse + # Check for siblings of the spouse if person.spouse.is_a?(Person) spouse_result = find_person_in_families(person.spouse.name) spouse_family = spouse_result[:child_of_family] if spouse_family spouse_siblings = find_siblings(spouse_family, person.spouse.name) - brothers_in_law.concat(spouse_siblings.select { |sibling| sibling.gender == Gender::MALE }.map(&:name)) + in_laws.concat(spouse_siblings.select { |sibling| sibling.gender == gender }.map(&:name)) end end unless child_of_family.nil? - # Check for male spouses of siblings of the person + # Check for spouses of siblings of the person siblings = find_siblings(child_of_family, person.name) siblings.each do |sibling| - brothers_in_law << sibling.spouse.name if sibling.spouse && sibling.spouse.gender == Gender::MALE + in_laws << sibling.spouse.name if sibling.spouse && sibling.spouse.gender == gender end end - brothers_in_law.uniq! - brothers_in_law.empty? ? 'NONE' : brothers_in_law.join(' ') + in_laws.uniq! + in_laws.empty? ? 'NONE' : in_laws.join(' ') end def find_person_in_families(name) |
