diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-10-24 21:10:57 +1100 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-10-24 21:10:57 +1100 |
| commit | 9bc26146397acb5a216e20d5eb55bb2a582fdd3e (patch) | |
| tree | 4c02f13ca30e673417870114050f7a3d653ad47d /lib/relationship_manager.rb | |
| parent | 55475178a8c0e610103e37027cc0a7a387d72f91 (diff) | |
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
Diffstat (limited to 'lib/relationship_manager.rb')
| -rw-r--r-- | lib/relationship_manager.rb | 21 |
1 files changed, 21 insertions, 0 deletions
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 |
