summaryrefslogtreecommitdiff
path: root/lib/family_tree.rb
blob: 66af74845407ca40facd59c3703488f28c565361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

require 'singleton'

require_relative 'person'
require_relative 'family_factory'

class FamilyTree
  include Singleton

  attr_accessor :families

  def initialize
    @families = FamilyFactory.new.create_families
  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 get_relationship(name, relationship)
    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_person_and_family(name)
    families.each do |family|
      family.children.each do |child|
        return { person: child, family: family } if child.name.casecmp(name).zero?
      end
    end
    { person: NilPerson.new, family: 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