GedNav/Individual.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 235 CC 22 SCAR 22.1
CC
22
Cov
0%
spouse
20.0
birth_death_years
20.0
private
12.0
CRAP 9.1
1 package GedNav::Individual;
2  
3 use File::Basename;
4  
5 use GedNav::Family;
6 use GedNav::Event;
7  
8 sub new
9 {
10 my $type = shift;
11 $type = ref $type if ref $type;
12 my $gedcom = shift;
13 my $code = uc(shift);
14  
15 my $newobj = {
16 code => $code,
17 gedcom => $gedcom,
18 };
19  
20 return bless $newobj, $type;
21 }
22  
23 sub gedcom
24 {
25 my $self = shift;
26 return $self->{'gedcom'};
27 }
28  
29 sub dataset
30 {
31 my $self = shift;
32 return $self->gedcom->dataset;
33 }
34  
35 sub raw
36 {
37 my $self = shift;
38 my $attrname = 'raw';
39  
40 unless (exists $self->{$attrname})
41 {
42 $self->{$attrname} = $self->gedcom->_get_paragraph("indi:" . $self->code);
43 }
44  
45 return $self->{$attrname};
46 }
47  
48 sub code
49 {
50 my $self = shift;
51 return $self->{'code'};
52 }
53  
54 sub private
55 {
56 my $self = shift;
57 my $attrname = 'private';
58  
59 if (@_)
60 {
61 $self->{$attrname} = shift;
62 }
63  
64 return 1 if $self->gedcom->private;
65 return $self->{$attrname};
66 }
67  
68 # assuming we're only a child in one family....
69 sub famc
70 {
71 my $self = shift;
72 my $attrname = 'famc';
73  
74 unless (exists $self->{$attrname})
75 {
76 my @matches =
77 map { new GedNav::Family($self->gedcom, $_) }
78 map { /^1\s+famc\s@(.*)@/i }
79 grep { /^1\s+famc/i }
80 @{$self->raw};
81  
82 warn "More than one famc!" if (@matches > 1);
83  
84 $self->{$attrname} = $matches[0];
85 }
86  
87 return $self->{$attrname};
88 }
89  
90 # there can easily be a spouse in more than one family -- returns a list
91 sub fams
92 {
93 my $self = shift;
94 my $attrname = 'fams';
95  
96 unless (exists $self->{$attrname})
97 {
98 my @matches =
99 map { new GedNav::Family($self->gedcom, $_) }
100 map { /^1\s+fams\s@(.*)@/i }
101 grep { /^1\s+fams/i }
102 @{$self->raw};
103  
104 $self->{$attrname} = \@matches;
105 }
106  
107 return @{$self->{$attrname}};
108 }
109  
110 sub sex
111 {
112 my $self = shift;
113 my $attrname = 'sex';
114  
115 unless (exists $self->{$attrname})
116 {
117 my @matches = grep { /^1\s+sex/i } @{$self->raw};
118 if (@matches)
119 {
120 ($self->{$attrname}) = ($matches[0] =~ /^1\s+sex\s+(.*)\s*$/i);
121 }
122 }
123  
124 return $self->{$attrname};
125 }
126  
127 sub child_type
128 {
129 my $self = shift;
130 return ($self->sex =~ /f/i) ? 'daughter' : 'son';
131 }
132  
133 sub name
134 {
135 my $self = shift;
136  
137 unless (exists $self->{'name'})
138 {
139 my @matches = grep { /^\d+\s+name/i } @{$self->raw};
140 if (@matches)
141 {
142 ($self->{'name'}) = ($matches[0] =~ /^\d+\s+name\s+(.*)\s*$/i);
143 }
144 }
145  
146 return $self->{'name'};
147 }
148  
149 sub name_html
150 {
151 my $self = shift;
152 my $name = $self->name;
153  
154 # also make the .* NOT greedy...
155 $name =~ s,/(.*)/, <i>$1</i>,g;
156  
157 return $name;
158 }
159  
160 sub name_html_linked
161 {
162 my $self = shift;
163  
164 return sprintf("<a href=\"individual.pl?dataset=%s&indi=%s\">%s</a>",
165 basename($self->dataset),
166 $self->code,
167 $self->name_html,
168 );
169 }
170  
171 sub name_normal
172 {
173 my $self = shift;
174 my $name = $self->name;
175  
176 # also make the .* NOT greedy...
177 $name =~ s,/(.*)/, $1 ,g;
178 $name =~ s, +, ,g;
179 $name =~ s,^ *,,g;
180 $name =~ s, *$,,g;
181  
182 return $name;
183 }
184  
185 sub name_reversed
186 {
187 my $self = shift;
188  
189 my $name = $self->name;
190  
191 $name =~ s!(.*)/(.*)/(.*)!$2, $1 $3!;
192  
193 return $name;
194 }
195  
196 # just returns 'first' spouse...
197 sub spouse
198 {
199 my $self = shift;
200  
201 my $fam;
202 $fam = $self->fams->[0];
203  
204 my $spouse;
205 if ($fam)
206 {
207 if ($self->sex =~ /^m$/i)
208 {
209 $spouse = $fam->wife;
210 }
211 elsif ($self->sex =~ /^f$/i)
212 {
213 $spouse = $fam->husb;
214 }
215 }
216  
217 return $spouse;
218 }
219  
220 sub surname
221 {
222 my $self = shift;
223 my ($surname) = $self->name =~ m,/(.*)/,;
224 return $surname;
225 }
226  
227 sub birth
228 {
229 my $self = shift;
230 my $attrname = 'birth';
231  
232 unless (exists $self->{$attrname})
233 {
234 $self->{$attrname} = new GedNav::Event('birt', $self);
235 }
236  
237 return $self->{$attrname}
238 }
239  
240 sub death
241 {
242 my $self = shift;
243 my $attrname = 'death';
244  
245 unless (exists $self->{$attrname})
246 {
247 $self->{$attrname} = new GedNav::Event('deat', $self);
248 }
249  
250 return $self->{$attrname}
251 }
252  
253 sub birth_death_years
254 {
255 my $self = shift;
256  
257 my $string = '';
258  
259 if ($self->birth && $self->death && $self->birth->year && $self->death->year)
260 {
261 $string = sprintf("%s - %s", $self->birth->year, $self->death->year);
262 }
263 elsif ($self->birth && $self->birth->year)
264 {
265 $string = sprintf("%s -", $self->birth->year);
266 }
267 elsif ($self->death && $self->death->year)
268 {
269 $string = sprintf("- %s", $self->death->year);
270 }
271  
272 return $string;
273 }
274  
275 sub has_children
276 {
277 my $self = shift;
278  
279 my $childcount = 0;
280  
281 foreach ($self->fams)
282 {
283 if ($_->children)
284 {
285 $childcount += scalar $_->children;
286 }
287 }
288  
289 return $childcount;
290 }
291  
292 ###
293  
294 1;
295