GedNav/Event.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 197 CC 22 SCAR 34.3
CC
22
Cov
0%
date_formatted
72.0
on_date_in_place
42.0
new
30.0
CRAP 30.9
1 package GedNav::Event;
2  
3 use strict;
4  
5 use Date::Manip;
6  
7 use GedNav::Source;
8  
9 sub new
10 {
11 my $type = shift;
12 $type = ref $type if ref $type;
13  
14 my $event_type = uc(shift);
15 my $parent = shift;
16  
17 my @paragraph = @{$parent->raw}; # copy so we can play with it
18  
19 while ( @paragraph && ($paragraph[0] !~ /1 $event_type/i) )
20 {
21 shift @paragraph;
22 }
23  
24 my $top = $paragraph[0] =~ /^(\d+)/;
25 shift @paragraph;
26  
27 my $newobj = {
28 type => $event_type,
29 parent => $parent,
30 };
31  
32 my @raw;
33  
34 my ($indent) = $paragraph[0] =~ /^(\d+)/;
35 while ( @paragraph && ($indent > $top) )
36 {
37 push @raw, $paragraph[0];
38  
39 if ($paragraph[0] =~ /^\d+\s+date\s+/i)
40 {
41 ($newobj->{'date'}) = $paragraph[0] =~ /^\d+\s+date\s+(.*)$/i;
42 }
43 elsif ($paragraph[0] =~ /^\d+\s+plac\s+/i)
44 {
45 ($newobj->{'place'}) = $paragraph[0] =~ /^\d+\s+plac\s+(.*)$/i;
46 }
47  
48 shift @paragraph;
49 ($indent) = $paragraph[0] =~ /^(\d+)/;
50 }
51  
52 return undef unless ($newobj->{'date'} || $newobj->{'place'});
53  
54 $newobj->{'raw'} = \@raw;
55  
56 return bless $newobj, $type;
57 }
58  
59 sub type
60 {
61 my $self = shift;
62 return $self->{'type'};
63 }
64  
65 sub parent
66 {
67 my $self = shift;
68 return $self->{'parent'};
69 }
70  
71 sub raw
72 {
73 my $self = shift;
74 return $self->{'raw'};
75 }
76  
77 sub gedcom
78 {
79 my $self = shift;
80 return $self->parent->gedcom;
81 }
82  
83 sub date
84 {
85 my $self = shift;
86 return $self->{'date'};
87 }
88  
89 sub date_formatted
90 {
91 my $self = shift;
92 my $format = shift;
93  
94 my $date = $self->date;
95  
96 if ($date =~ /private/i)
97 {
98 $date = '';
99 }
100 elsif ($date =~ /wft/i)
101 {
102 $date =~ s/.*(\d{4}\s*-\s*\d{4})/Abt $1/;
103 }
104 else
105 {
106 my $prefix = '';
107  
108 if ($date =~ /(abt|est)/i)
109 {
110 $prefix = 'About ';
111 $date =~ s/(abt|est)\.?//i;
112 }
113  
114 if ($date =~ /aft/i)
115 {
116 $prefix = 'After ';
117 $date =~ s/aft\.?//i;
118 }
119  
120 if ($date =~ /bef/i)
121 {
122 $prefix = 'Before ';
123 $date =~ s/bef\.?//i;
124 }
125  
126 $date =~ s/^ +//g;
127 $date =~ s/ +$//g;
128  
129 if ($date =~ /^\d{1,2} \w{3} \d{4}$/i)
130 {
131 $date = UnixDate($date, $format || "%e %b %Y");
132 }
133 elsif ($date =~ /\d{4}$/)
134 {
135 $date =~ s/^.*(\d{4})$/$1/;
136 }
137  
138 $date = $prefix . $date;
139 }
140  
141 return $date;
142 }
143  
144 sub year
145 {
146 my $self = shift;
147  
148 my ($year) = $self->date =~ /(\d{4})/;
149  
150 return $year;
151 }
152  
153 sub place
154 {
155 my $self = shift;
156 return $self->{'place'};
157 }
158  
159 sub date_in_place
160 {
161 my $self = shift;
162  
163 my $string;
164  
165 if ($self->date && $self->place)
166 {
167 $string = sprintf("%s in %s", $self->date_formatted, $self->place);
168 }
169 elsif ($self->date)
170 {
171 $string = $self->date_formatted;
172 }
173 elsif ($self->place)
174 {
175 $string = sprintf("in %s", $self->place);
176 }
177  
178 return $string;
179 }
180  
181 sub on_date_in_place
182 {
183 my $self = shift;
184  
185 my $string;
186  
187 if ($self->date && $self->place)
188 {
189 if ($self->date_formatted =~ /^\d+$/)
190 {
191 $string = "in " . $self->date_in_place;
192 }
193 else
194 {
195 $string = "on " . $self->date_in_place;
196 }
197 }
198 elsif ($self->date)
199 {
200 if ($self->date_formatted =~ /^\d+$/)
201 {
202 $string = "in " . $self->date_formatted;
203 }
204 else
205 {
206 $string = "on " . $self->date_formatted;
207 }
208 }
209 elsif ($self->place)
210 {
211 $string = "in " . $self->place;
212 }
213  
214 return $string;
215 }
216  
217 sub sources
218 {
219 my $self = shift;
220 my $attrname = 'sour';
221  
222 unless (exists $self->{$attrname})
223 {
224 my @matches =
225 map { new GedNav::Source($self, $_) }
226 map { /^2\s+sour\s+@(.*)@/i }
227 grep { /^2\s+sour/i }
228 @{$self->raw};
229  
230 $self->{$attrname} = \@matches;
231 }
232  
233 return @{$self->{$attrname}} if wantarray;
234 return $self->{$attrname};
235 }
236  
237 ###
238  
239 1;
240