diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-10-24 22:03:54 +1100 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-10-24 22:03:54 +1100 |
| commit | a89fa6a27bb0e2b9797dd01052aaf22b38296821 (patch) | |
| tree | 2426ca3a9f1a0103c2d4efcd4c2e8fe6ab5717ac /lib/family_tree.rb | |
| parent | 9bc26146397acb5a216e20d5eb55bb2a582fdd3e (diff) | |
Add tests for FamilyTree class
- return appropriate error messages when person isn't found or if there aren't any siblings
Diffstat (limited to 'lib/family_tree.rb')
| -rw-r--r-- | lib/family_tree.rb | 50 |
1 files changed, 26 insertions, 24 deletions
diff --git a/lib/family_tree.rb b/lib/family_tree.rb index ebb2955..66af748 100644 --- a/lib/family_tree.rb +++ b/lib/family_tree.rb @@ -8,11 +8,10 @@ require_relative 'family_factory' class FamilyTree include Singleton - attr_accessor :families, :people + attr_accessor :families def initialize @families = FamilyFactory.new.create_families - @people = [] end def add_family(family) @@ -24,35 +23,38 @@ class FamilyTree end def get_relationship(name, relationship) - family = find_family(name) - - return [] if family.nil? - - if child_of_family?(family, name) - case relationship.downcase - when 'mother' - mother = find_mother(family) - return mother.is_a?(NilPerson) ? [] : [mother] - when 'father' - father = find_father(family) - return father.is_a?(NilPerson) ? [] : [father] - when 'siblings' - return find_siblings(family, name) - else - return [] - end + result = find_person_and_family(name) + person = result[:person] + family = result[:family] + + return 'PERSON_NOT_FOUND' if family.nil? || person.is_a?(NilPerson) + + case relationship.downcase + when 'mother' + mother = find_mother(family) + return 'PERSON_NOT_FOUND' if mother.is_a?(NilPerson) + + mother.name + when 'father' + father = find_father(family) + return 'PERSON_NOT_FOUND' if father.is_a?(NilPerson) + + father.name + when 'siblings' + siblings = find_siblings(family, name) + siblings.empty? ? 'NONE' : siblings.map(&:name).join(' ') + else + 'UNSUPPORTED_RELATIONSHIP' end - - [] end - def find_family(name) + def find_person_and_family(name) families.each do |family| family.children.each do |child| - return family if child.name.casecmp(name).zero? + return { person: child, family: family } if child.name.casecmp(name).zero? end end - nil + { person: NilPerson.new, family: nil } end def find_mother(family) |
