File Coverage

blib/lib/Genealogy/AncestorChart.pm
Criterion Covered Total %
statement 53 67 79.1
branch 8 12 66.6
condition n/a
subroutine 13 15 86.6
pod 8 8 100.0
total 82 102 80.3


\n]; \n", @cells, "\n"; "} @{ $self->table_headers };
line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Genealogy::AncestorChart - create a table of genealogical ancestors.
4              
5             =head1 SYNOPSIS
6              
7             use Genealogy::AncestorChart;
8              
9             my @people = ($me, $dad, $mum, ...);
10              
11             my $chart = Genealogy::AncestorChart->new({
12             people => \@people,
13             });
14              
15             say $gac->chart;
16              
17             =head1 DESCRIPTION
18              
19             This module draws an HTML table which contains a representation of ancestors.
20             For the first three generations, the table will look like this:
21              
22             +-------------+-------------+----------------+
23             | Person | Parents | Grandparents |
24             +-------------+-------------+----------------+
25             | 1: Person | 2: Father | 4: Grandfather |
26             | | +----------------+
27             | | | 5: Grandmother |
28             | +-------------+----------------+
29             | | 3: Mother | 6: Grandfather |
30             | | +----------------+
31             | | | 7: Grandmother |
32             +-------------+-------------+----------------+
33              
34             The labels inside the table are generated from the array of people that is
35             passed into the constructor when the object is created.
36              
37             =head1 METHODS
38              
39             =head2 new(\%options)
40              
41             The constructor method. Builds a new instance of the class and returns that
42             object. Takes a hash reference containing various parameters.
43              
44             =over 4
45              
46             =item * people
47              
48             This is the only mandatory option.
49              
50             This should be a reference to a hash of objects. The key in the hash should
51             be an "Ahnentafel number" (see below) and the value should be an object will
52             represent one person to be displayed in the output. The text displayed
53             for each person is retrieved by calling a method on the relevant object.
54             The default method name is C, but this can be changed
55             using the C option, described below.
56              
57             These objects can be of any class - as long as they have a method of the
58             correct name that returns a text string that can be used in the table
59             output to identify the person in question.
60              
61             The keys in the hash should be "Ahnentafel numbers". This is a series
62             of positive integers that genealogists use to identify the ancestors of
63             an individual. The person of interest is given the number 1. Their
64             father and mother are 2 and 3, respectively. Their grandparents are
65             numbers 4 to 7 in the order father's father, father's mother, mother's
66             father and mother's mother. These numbers have the property that if you
67             know the number of a person in the hash, then you can get the number of
68             their father by doubling their number. Similarly, you can get the number
69             of their mother by doubling their number and adding one.
70              
71             Because of the nature of the table that is produced, the number of
72             people in your array should be one less than a power of two (i.e. 1, 3,
73             7, 15, 31, 63, etc.) For any any other number, a table will still be
74             produced, but it won't be guaranteed to be valid HTML.
75              
76             In the future, I might introduce a "strict" mode that only allows a valid
77             number of people in the array.
78              
79             =item * label_method
80              
81             This is the name of the method that should be called on the objects in the
82             "people" array. The default value is C.
83              
84             =item * headers
85              
86             An array reference containing the list of titles that are used for the first
87             few columns in the table. The default list is 'Person', 'Parents',
88             'Grandparents' and 'Great Grandparents'. You might want to override this if,
89             for example, you want the output in a different language.
90              
91             =item * extra_headers
92              
93             A string containing the basis for an extra headers that are required after
94             the fixed list stored in C. In English, we use the terms
95             'Great Great Grandparents', 'Great Great Great Grandparents' and so on. So
96             the default value for this string is 'Gt Grandparents'. This is prepended
97             with an incrementing string (which starts at 2) so we get the strings
98             '2 Gt Grandparents', '3 Gt Grandparents', and so on.
99              
100             You might want to override this if, for example, you want the output in a
101             different language.
102              
103             =back
104              
105             =cut
106              
107             package Genealogy::AncestorChart;
108              
109 2     2   49150 use strict;
  2         11  
  2         43  
110 2     2   8 use warnings;
  2         2  
  2         58  
111              
112             our $VERSION = '0.0.1';
113              
114 2     2   1448 use Moo;
  2         16444  
  2         6  
115 2     2   3558 use Types::Standard qw[ArrayRef HashRef Str Object];
  2         117673  
  2         26  
116              
117             has people => (
118             is => 'ro',
119             isa => HashRef[Object],
120             required => 1,
121             );
122              
123             =head2 num_people
124              
125             Returns the number of people in the list of people.
126              
127             =cut
128              
129             sub num_people {
130 3     3 1 3 return keys %{ $_[0]->people };
  3         17  
131             }
132              
133             has label_method => (
134             is => 'lazy',
135             isa => Str,
136             );
137              
138             sub _build_label_method {
139 1     1   20 return 'display_name',
140             }
141              
142             has headers => (
143             is => 'lazy',
144             isa => ArrayRef[Str],
145             );
146              
147             sub _build_headers {
148             return [
149 1     1   20 'Person', 'Parents', 'Grandparents',
150             'Great Grandparents',
151             ];
152             }
153              
154             has extra_header => (
155             is => 'lazy',
156             isa => Str,
157             );
158              
159             sub _build_extra_header {
160 0     0   0 return 'Gt Grandparents';
161             }
162              
163             =head2 num_rows
164              
165             Returns the number of rows that will be in the table. This is calculated
166             from the list of people.
167              
168             It is unlikely that you will need to call this method.
169              
170             =cut
171              
172             sub num_rows {
173 1     1 1 14 my $self = shift;
174              
175 1         2 return int ( keys( %{ $self->people } ) / 2 ) + 1;
  1         7  
176             }
177              
178             =head2 rows
179              
180             Returns the list of rows that will be used to create the table.
181              
182             It is unlikely that you will need to call this method.
183              
184             =cut
185              
186             sub rows {
187 1     1 1 3774 my $self = shift;
188              
189 1         4 my ($start, $end) = $self->row_range;
190              
191 1         2 return map { $self->row($_) } $start .. $end;
  4         8  
192             }
193              
194             =head2 row_range
195              
196             Returns a start and end point that is used in creating the rows of
197             the table.
198              
199             It is unlikely that you will need to call this method.
200              
201             =cut
202              
203             sub row_range {
204 1     1 1 2 my $self = shift;
205              
206 1         1 my $end = keys %{ $self->people };
  1         4  
207 1         4 my $start = int(($end / 2) + 1);
208              
209 1         3 return ($start, $end);
210             }
211              
212             =head2 num_cols
213              
214             Returns the number of columns that will be in the table. This is calculated
215             from the list of people.
216              
217             It is unlikely that you will need to call this method.
218              
219             =cut
220             sub num_cols {
221 3     3 1 5 my $self = shift;
222              
223 3         6 return int log( $self->num_people ) / log(2) + 1;
224             }
225              
226             =head2 row
227              
228             Returns the HTML that makes up one row in the table.
229              
230             It is unlikely that you will need to call this method.
231              
232             =cut
233              
234             sub row {
235 4     4 1 5 my $self = shift;
236 4         7 my ($rownum) = @_;
237              
238 4         3 my @cells;
239 4         5 my $rowspan = 1;
240 4         4 my $i = $rownum;
241              
242 4         61 my $label_method = $self->label_method;
243              
244 4         47 while (1) {
245 7 50       26 my $person = exists $self->people->{$i} ? $self->people->{$i} : undef;
246 7 100       17 my $class = $person ? $person->known ? 'success' : 'danger' : 'danger';
    50          
247 7 50       65 my $desc = "$i: " . ($person ? $person->$label_method : '');
248 7         23 my $td = qq[$desc
249 7         13 unshift @cells, $td;
250              
251 7 100       14 last if $i % 2;
252 3         3 $rowspan *= 2;
253 3         5 $i /= 2;
254             }
255              
256 4         14 return join '', "
257             }
258              
259             =head2 table_headers
260              
261             Calculates and returns the headers used in the table.
262              
263             =cut
264              
265             sub table_headers {
266 1     1 1 2 my $self = shift;
267              
268 1         2 my @headers;
269 1 50       2 if ($self->num_cols <= @{ $self->headers }) {
  1         20  
270 1         41 @headers = @{ $self->headers }[0 .. $self->num_cols - 1];
  1         14  
271             } else {
272 0         0 @headers = @{ $self->headers };
  0         0  
273 0         0 my $gt = 2;
274 0         0 for (@headers .. $self->num_cols) {
275 0         0 push @headers, $gt++ . ' ' . $self->extra_header;
276             }
277             }
278              
279 1         12 return \@headers;
280             }
281              
282             =head2 chart
283              
284             Returns the complete HTML of the ancestor chart.
285              
286             =cut
287              
288             sub chart {
289 0     0 1   my $self = shift;
290              
291 0           my $headers = join "\n", map { "$_
  0            
  0            
292              
293 0           my $table = <
294             \n
295            
296            
297             $headers
298            
299            
300            
301             EOTABLE
302              
303 0           $table .= join '', $self->rows;
304              
305 0           $table .= "\n
";
306              
307 0           return $table;
308             }
309              
310             =head1 AUTHOR
311              
312             Dave Cross
313              
314             =head1 COPYRIGHT AND LICENCE
315              
316             Copyright (c) 2022, Magnum Solutions Ltd. All Rights Reserved.
317              
318             This library is free software; you can redistribute it and/or modify it
319             under the same terms as Perl itself.
320              
321             =cut
322              
323             1;