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/family.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/family.rb')
| -rw-r--r-- | lib/family.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/family.rb b/lib/family.rb new file mode 100644 index 0000000..2c1acca --- /dev/null +++ b/lib/family.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require_relative 'person' + +class Family + attr_reader :mother, :father, :children + + def initialize(mother = NilPerson.new, father = NilPerson.new, children = []) + raise ArgumentError, 'Mother must be female' if !mother.is_a?(NilPerson) && mother.gender != Gender::FEMALE + raise ArgumentError, 'Father must be male' if !father.is_a?(NilPerson) && father.gender != Gender::MALE + + @mother = mother + @father = father + @children = children + end + + def assign_mother(mother) + raise ArgumentError, 'Mother must be female' if mother.gender != Gender::FEMALE + + @mother = mother + end + + def assign_father(father) + raise ArgumentError, 'Father must be male' if father.gender != Gender::MALE + + @father = father + end + + def add_child(child) + @children << child unless @children.include?(child) + end + + def get_siblings(person) + @children.reject { |child| child == person } + end + + def to_s + mother_str = mother.is_a?(NilPerson) ? 'UNKNOWN' : mother.name + father_str = father.is_a?(NilPerson) ? 'UNKNOWN' : father.name + children_str = children.empty? ? 'NONE' : children.map(&:name).join(', ') + "Family: Mother: #{mother_str}, Father: #{father_str}, Children: #{children_str}" + end +end |
