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_tree.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_tree.rb')
| -rw-r--r-- | lib/family_tree.rb | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/lib/family_tree.rb b/lib/family_tree.rb index 934d32e..ebb2955 100644 --- a/lib/family_tree.rb +++ b/lib/family_tree.rb @@ -1,15 +1,79 @@ # frozen_string_literal: true +require 'singleton' + +require_relative 'person' +require_relative 'family_factory' + class FamilyTree + include Singleton + + attr_accessor :families, :people + def initialize + @families = FamilyFactory.new.create_families @people = [] end + def add_family(family) + @families << family unless @families.include?(family) + end + def add_child(*params) puts "Adding Child with params: #{params.join(', ')}" end - def query_hierarchy(*params) - puts "Querying Hierarcy with params: #{params.join(', ')}" + 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 + end + + [] + end + + def find_family(name) + families.each do |family| + family.children.each do |child| + return family if child.name.casecmp(name).zero? + end + end + nil + end + + def find_mother(family) + mother = family.mother + mother || NilPerson.new + end + + def find_father(family) + father = family.father + father || NilPerson.new + end + + def find_siblings(family, name) + family.children.reject { |child| child.name.casecmp(name).zero? } + end + + def child_of_family?(family, name) + return false unless family.is_a?(Family) + + family.children.any? do |child| + child.name.casecmp(name).zero? + end end end |
