-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.rb
129 lines (111 loc) · 3.41 KB
/
home.rb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class Home
attr_accessor :family
def initialize
@family =[]
end
def find_parents(person)
parents=[]
if person.parents.length == 2 && patner_exists?(person)
patner = find_patner(person)
parents = [person, patner]
elsif person.parents.length == 1
parents = [person.parents.first, person]
end
parents
end
def patner_exists?(person)
if find_patner(person) == nil
return false
else
return true
end
end
def find_patner(person)
self.family.find{ |member| !member.parents.empty? && member.parents.all? { |x| x == person } }
end
def find_son(person)
sons= []
self.family.each do |member|
sons << member if ( member.parents.length > 1 && member.gender == 'Male' && member.parents.include?(person))
end
sons
end
def find_daughter(person)
daughters = []
self.family.each do |member|
daughters << member if ( member.parents.length > 1 && member.gender == 'Female' && member.parents.include?(person))
end
daughters
end
def find_siblings(person)
siblings = []
self.family.each do |member|
siblings << member if ( member.parents == person.parents && member != person)
end
siblings
end
def find_sisters(person)
sisters = []
return sisters if person.nil?
self.family.each do |member|
sisters << member if ( member.gender == "Female" && member.parents === person.parents && member != person )
end
sisters
end
def find_brothers(person)
brothers = []
return brothers if person.nil?
self.family.each do |member|
brothers << member if ( member.gender == "Male" && member.parents === person.parents && member != person )
end
brothers
end
def find_maternal_uncle(person)
find_brothers(person.parents.find{ |parent| parent.parents.length > 1 && parent.gender == 'Female'})
end
def find_paternal_uncle(person)
find_brothers(person.parents.find{ |parent| (parent.parents.length > 1 && parent.gender =='Male')})
end
def find_maternal_aunt(person)
find_sisters(person.parents.find{ |parent| parent.parents.length > 1 && parent.gender == 'Female'})
end
def find_paternal_aunt(person)
find_sisters(person.parents.find{ |parent| parent.parents.length > 1 && parent.gender == 'Male'})
end
def find_brother_in_law(person)
if person.parents.length == 1 # Spouse’s brothers
find_brothers(person.parents.first)
elsif person.parents.length == 2 # Husbands of siblings
find_husbands_of_siblings(person)
else
return []
end
end
def find_sister_in_law(person)
if person.parents.length == 1 # Spouse's sisters
find_sisters(person.parents.first)
elsif person.parents.length == 2 # wives of siblings
find_wives_of_siblings(person)
else
return []
end
end
def find_husbands_of_siblings(person)
husbands_of_siblings =[]
sisters = find_sisters(person)
return [] if sisters.empty?
self.family.each do |member|
husbands_of_siblings << member if member.parents.length == 1 && member.gender == 'Male' && sisters.any?{ |x| member.parents.include?(x) }
end
husbands_of_siblings
end
def find_wives_of_siblings(person)
wives_of_siblings =[]
brothers = find_brothers(person)
return [] if brothers.empty?
self.family.each do |member|
wives_of_siblings << member if member.parents.length == 1 && member.gender == 'Female' && brothers.any?{ |x| member.parents.include?(x) }
end
wives_of_siblings
end
end