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/family_tree.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'lib/family_tree.rb') 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 -- cgit v1.2.3