From 9bc26146397acb5a216e20d5eb55bb2a582fdd3e Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Thu, 24 Oct 2024 21:10:57 +1100 Subject: Implement key data model - Added classes for Person, Gender, Family, FamilyTree - Replaced FamilyTreeManager with FamilyTree - Add FamilyFactory for seeding the initial FamilyTree for King Arthur and Queen Margaret - Added a RelationshipManager for linking spouses correctly - Refactored ActionFileExecutor for readability - More test coverage --- lib/relationship_manager.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 lib/relationship_manager.rb (limited to 'lib/relationship_manager.rb') diff --git a/lib/relationship_manager.rb b/lib/relationship_manager.rb new file mode 100644 index 0000000..4b6a4a2 --- /dev/null +++ b/lib/relationship_manager.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'singleton' + +class RelationshipManager + include Singleton + def link_spouses(person1, person2) + # Check if either person is already linked to someone else + if person1.spouse != NilPerson.new && person1.spouse != person2 + raise "Cannot link #{person1.name} and #{person2.name}: #{person1.name} is already linked to #{person1.spouse.name}." + end + + if person2.spouse != NilPerson.new && person2.spouse != person1 + raise "Cannot link #{person1.name} and #{person2.name}: #{person2.name} is already linked to #{person2.spouse.name}." + end + + # Link the spouses + person1.spouse = person2 + person2.spouse = person1 unless person2.is_a?(NilPerson) + end +end -- cgit v1.2.3