| 1 | package Genealogy::Ahnentafel; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Genealogy::Ahnentafel - Handle Ahnentafel numbers in Perl. | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use Genealogy::Ahnentafel; | ||||||||||
| 10 | |||||||||||
| 11 | my $ahnen = ahnen(1); | ||||||||||
| 12 | say $ahnen->gen; # 1 | ||||||||||
| 13 | say $ahnen->gender; # Unknown | ||||||||||
| 14 | say $ahnen->description; # Person | ||||||||||
| 15 | |||||||||||
| 16 | my $ahnen = ahnen(2); | ||||||||||
| 17 | say $ahnen->gen; # 2 | ||||||||||
| 18 | say $ahnen->gender; # Male | ||||||||||
| 19 | say $ahnen->description; # Father | ||||||||||
| 20 | |||||||||||
| 21 | my $ahnen = ahnen(5); | ||||||||||
| 22 | say $ahnen->gen; # 3 | ||||||||||
| 23 | say $ahnen->gender; # Female | ||||||||||
| 24 | say $ahnen->description; # Grandmother | ||||||||||
| 25 | |||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||
| 27 | |||||||||||
| 28 | Geologists often use Ahnentafel (from the German for "ancestor table") | ||||||||||
| 29 | numbers to identify the direct ancestors of a person. The original | ||||||||||
| 30 | person of interest is given the number 1, their father and mother are | ||||||||||
| 31 | 2 and 3, their paternal grandparents are 4 and 5, their maternal | ||||||||||
| 32 | grandparents are 6 and 7 and the list goes on. | ||||||||||
| 33 | |||||||||||
| 34 | This class gives you a way to deal with these numbers in Perl. | ||||||||||
| 35 | |||||||||||
| 36 | Ahnentafel numbers have some interesting properties. For example, with | ||||||||||
| 37 | the exception of the first person in the list (who can, obviously, be | ||||||||||
| 38 | of either sex) all of the men have Ahnentafel numbers which are even | ||||||||||
| 39 | and the women have Ahnentafel numbers which are even. You can calculate | ||||||||||
| 40 | the number of the father of any person on the list simply by doubling | ||||||||||
| 41 | the number of the child. You can get the number of their mother by | ||||||||||
| 42 | doubling the child's number and adding one. | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | 1 | our $VERSION = '1.0.3'; | |||||||||
| 50 | |||||||||||
| 51 | 1 | require Exporter; | |||||||||
| 52 | 1 | our @ISA = qw[Exporter]; | |||||||||
| 53 | 1 | our @EXPORT = qw[ahnen]; | |||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | ▶ | use MooX::ClassAttribute; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 59 | 1 | ▶ | use Types::Standard qw( Str Int ArrayRef Bool ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 1 | ▶ | use Type::Utils qw( declare as where inline_as coerce from ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | my $PositiveInt = declare | ||||||||||
| 63 | as Int, | ||||||||||
| 64 | 0 | ▶ | where { $_ > 0 }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 65 | 4 | ▶ | inline_as { "defined $_ and $_ =~ /^[0-9]+\$/ and $_ > 0" }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 66 | |||||||||||
| 67 | use overload | ||||||||||
| 68 | 14 | ▶ | '""' => sub { $_[0]->ahnentafel }, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 69 | 1 | fallback => 1; | |||||||||
| 70 | |||||||||||
| 71 | =head1 FUNCTIONS | ||||||||||
| 72 | |||||||||||
| 73 | This module exports one function. | ||||||||||
| 74 | |||||||||||
| 75 | =head2 ahnen($positive_integer) | ||||||||||
| 76 | |||||||||||
| 77 | This function takes a positive integer and returns a Genealogy::Ahnentafel | ||||||||||
| 78 | object for that integer. If you pass it something that isn't a positive | ||||||||||
| 79 | integer the function will throw an exception. | ||||||||||
| 80 | |||||||||||
| 81 | This is just a short-cut for | ||||||||||
| 82 | |||||||||||
| 83 | Genealogy::Ahnentafel->new({ ahnentafel => $positive_integer }) | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 53 | ▶ | sub ahnen { | ||||||||
|
Subroutine
ahnen: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 88 | 53 | return Genealogy::Ahnentafel->new({ ahnentafel => $_[0] }); | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | =head1 CLASS ATTRIBUTES | ||||||||||
| 92 | |||||||||||
| 93 | The module provides two class attributes. These define strings that are | ||||||||||
| 94 | used in the output of various methods in the class. They are provided to | ||||||||||
| 95 | make it easier to subclass this class to support internationalisation. | ||||||||||
| 96 | |||||||||||
| 97 | =head2 genders | ||||||||||
| 98 | |||||||||||
| 99 | This is a reference to an array that contains two strings that represent | ||||||||||
| 100 | the genders male and female. By default, they are the strings "Male" and | ||||||||||
| 101 | "Female". | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | 1 | class_has genders => ( | |||||||||
| 106 | is => 'lazy', | ||||||||||
| 107 | isa => ArrayRef[Str], | ||||||||||
| 108 | ); | ||||||||||
| 109 | |||||||||||
| 110 | 1 | ▶ | sub _build_genders { | ||||||||
|
Subroutine
_build_genders: called CC=1 SCAR=0.0
| |||||||||||
| 111 | 1 | return [ qw[Male Female] ]; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | =head2 parent_names | ||||||||||
| 115 | |||||||||||
| 116 | This is a reference to an array that contains two strings that represent | ||||||||||
| 117 | the parent of the two genders. By default, they are the strings "Father" | ||||||||||
| 118 | and "Mother". | ||||||||||
| 119 | |||||||||||
| 120 | Note that these strings are also used to build more complex relationship | ||||||||||
| 121 | names like "Grandfather" and "Great Grandmother". | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 1 | class_has parent_names => ( | |||||||||
| 126 | is => 'lazy', | ||||||||||
| 127 | isa => ArrayRef[Str], | ||||||||||
| 128 | ); | ||||||||||
| 129 | |||||||||||
| 130 | 1 | ▶ | sub _build_parent_names { | ||||||||
|
Subroutine
_build_parent_names: called CC=1 SCAR=0.0
| |||||||||||
| 131 | 1 | return [ qw[Father Mother] ]; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | =head1 OBJECT ATTRIBUTES | ||||||||||
| 135 | |||||||||||
| 136 | Objects of this class have the following attributes. Most them are | ||||||||||
| 137 | lazily generated from the Ahnentafel number. | ||||||||||
| 138 | |||||||||||
| 139 | =head2 ahnentafel | ||||||||||
| 140 | |||||||||||
| 141 | The positive integer that was used to create this object. | ||||||||||
| 142 | |||||||||||
| 143 | say ahnen(123)->ahnentafel; # 123 | ||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 1 | has ahnentafel => ( | |||||||||
| 148 | is => 'ro', | ||||||||||
| 149 | isa => $PositiveInt, | ||||||||||
| 150 | required => 1, | ||||||||||
| 151 | ); | ||||||||||
| 152 | |||||||||||
| 153 | =head2 gender | ||||||||||
| 154 | |||||||||||
| 155 | The gender of the person represented by this object. This returns "Unknown" | ||||||||||
| 156 | for person 1 (as the person at the root of the tree can be of either gender). | ||||||||||
| 157 | Other than that people with an even Ahnentafel number are men and people with | ||||||||||
| 158 | an odd Ahnentafel are women. | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 1 | has gender => ( | |||||||||
| 163 | is => 'lazy', | ||||||||||
| 164 | isa => Str, | ||||||||||
| 165 | ); | ||||||||||
| 166 | |||||||||||
| 167 | 7 | ▶ | sub _build_gender { | ||||||||
|
Subroutine
_build_gender: called CC=2 SCAR=6.9
| |||||||||||
| 168 | 7 | my $ahnen = $_[0]->ahnentafel; | |||||||||
| 169 | 7 | ▶ | return 'Unknown' if $ahnen == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 6 | return $_[0]->genders->[ $ahnen % 2 ]; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | =head2 gender_description | ||||||||||
| 174 | |||||||||||
| 175 | (I'm not convinced by this name. I'll almost certainly change it at some | ||||||||||
| 176 | point.) | ||||||||||
| 177 | |||||||||||
| 178 | The base word that is used for people of this gender. It is "Person" for | ||||||||||
| 179 | person 1 (as we don't know their gender) and either "Father" or "Mother" | ||||||||||
| 180 | as appropriate for everyone else. | ||||||||||
| 181 | |||||||||||
| 182 | =cut | ||||||||||
| 183 | |||||||||||
| 184 | 1 | has gender_description => ( | |||||||||
| 185 | is => 'lazy', | ||||||||||
| 186 | isa => Str, | ||||||||||
| 187 | ); | ||||||||||
| 188 | |||||||||||
| 189 | 10 | ▶ | sub _build_gender_description { | ||||||||
|
Subroutine
_build_gender_description: called CC=2 SCAR=7.1
| |||||||||||
| 190 | 10 | my $ahnen = $_[0]->ahnentafel; | |||||||||
| 191 | 10 | ▶ | return 'Person' if $ahnen == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 10 | return $_[0]->parent_names->[ $ahnen % 2 ]; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | =head2 generation | ||||||||||
| 196 | |||||||||||
| 197 | The number of the generation that this person is in. Person 1 is in | ||||||||||
| 198 | generation 1. People 2 and 3 (the parents) are in generation 2. People | ||||||||||
| 199 | 4 to 7 (the grandparents) are in generation 3. And so on. | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||
| 202 | |||||||||||
| 203 | 1 | has generation => ( | |||||||||
| 204 | is => 'lazy', | ||||||||||
| 205 | isa => $PositiveInt, | ||||||||||
| 206 | ); | ||||||||||
| 207 | |||||||||||
| 208 | 22 | ▶ | sub _build_generation { | ||||||||
|
Subroutine
_build_generation: called CC=1 SCAR=0.0
| |||||||||||
| 209 | 22 | my $ahnen = $_[0]->ahnentafel; | |||||||||
| 210 | 22 | return int log( $ahnen ) / log(2) + 1; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | =head2 description | ||||||||||
| 214 | |||||||||||
| 215 | A description of the relationship between the root person and the current | ||||||||||
| 216 | person. For person 1, it is "Person". For people 2 and 3 it is "Father" | ||||||||||
| 217 | or "Mother". For people in generation 3, it is "Grandfather" or | ||||||||||
| 218 | "Grandmother". After that we prepend the appropriate number of repetitions | ||||||||||
| 219 | of "Great" - "Great Grandmother", "Great Great Grandfather", etc. | ||||||||||
| 220 | |||||||||||
| 221 | =cut | ||||||||||
| 222 | |||||||||||
| 223 | 1 | has description => ( | |||||||||
| 224 | is => 'lazy', | ||||||||||
| 225 | isa => Str, | ||||||||||
| 226 | ); | ||||||||||
| 227 | |||||||||||
| 228 | 12 | ▶ | sub _build_description { | ||||||||
|
Subroutine
_build_description: called CC=4 SCAR=13.9
| |||||||||||
| 229 | 12 | my $ahnen = $_[0]->ahnentafel; | |||||||||
| 230 | |||||||||||
| 231 | 12 | my $generation = $_[0]->generation(); | |||||||||
| 232 | |||||||||||
| 233 | 12 | ▶ | return 'Person' if $generation == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 234 | |||||||||||
| 235 | 10 | my $root = $_[0]->gender_description; | |||||||||
| 236 | 10 | ▶ | return $root if $generation == 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 237 | 7 | $root = "Grand\L$root"; | |||||||||
| 238 | 7 | ▶ | return $root if $generation == 3; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 239 | 2 | my $greats = $generation - 3; | |||||||||
| 240 | 2 | return ('Great ' x $greats) . $root; | |||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | =head2 ancestry | ||||||||||
| 244 | |||||||||||
| 245 | An array of Genealogy::Ahnentafel objects representing all of the people | ||||||||||
| 246 | between (and including) the root person and the current person. | ||||||||||
| 247 | |||||||||||
| 248 | =cut | ||||||||||
| 249 | |||||||||||
| 250 | 1 | has ancestry => ( | |||||||||
| 251 | is => 'lazy', | ||||||||||
| 252 | isa => ArrayRef, | ||||||||||
| 253 | ); | ||||||||||
| 254 | |||||||||||
| 255 | 1 | ▶ | sub _build_ancestry { | ||||||||
|
Subroutine
_build_ancestry: called CC=2 SCAR=6.9
| |||||||||||
| 256 | 1 | my @ancestry; | |||||||||
| 257 | 1 | my $curr = $_[0]->ahnentafel; | |||||||||
| 258 | |||||||||||
| 259 | 1 | ▶ | while ($curr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 260 | 3 | unshift @ancestry, ahnen($curr); | |||||||||
| 261 | 3 | $curr = int($curr / 2); | |||||||||
| 262 | } | ||||||||||
| 263 | |||||||||||
| 264 | 1 | return \@ancestry; | |||||||||
| 265 | } | ||||||||||
| 266 | |||||||||||
| 267 | =head2 ancestry_string | ||||||||||
| 268 | |||||||||||
| 269 | A string representation of ancestry. | ||||||||||
| 270 | |||||||||||
| 271 | =cut | ||||||||||
| 272 | |||||||||||
| 273 | 1 | has ancestry_string => ( | |||||||||
| 274 | is => 'lazy', | ||||||||||
| 275 | isa => Str, | ||||||||||
| 276 | ); | ||||||||||
| 277 | |||||||||||
| 278 | 1 | ▶ | sub _build_ancestry_string { | ||||||||
|
Subroutine
_build_ancestry_string: called CC=1 SCAR=0.0
| |||||||||||
| 279 | 1 | return join ', ', map { $_->description } @{ $_[0]->ancestry }; | |||||||||
| 280 | } | ||||||||||
| 281 | |||||||||||
| 282 | =head2 father | ||||||||||
| 283 | |||||||||||
| 284 | A Genealogy::Ahnentafel object representing the father of the current | ||||||||||
| 285 | person. | ||||||||||
| 286 | |||||||||||
| 287 | =cut | ||||||||||
| 288 | |||||||||||
| 289 | 1 | has father => ( | |||||||||
| 290 | is => 'lazy', | ||||||||||
| 291 | ); | ||||||||||
| 292 | |||||||||||
| 293 | 4 | ▶ | sub _build_father { | ||||||||
|
Subroutine
_build_father: called CC=1 SCAR=0.0
| |||||||||||
| 294 | 4 | return ahnen($_[0]->ahnentafel * 2); | |||||||||
| 295 | } | ||||||||||
| 296 | |||||||||||
| 297 | =head2 mother | ||||||||||
| 298 | |||||||||||
| 299 | A Genealogy::Ahnentafel object representing the mother of the current | ||||||||||
| 300 | person. | ||||||||||
| 301 | |||||||||||
| 302 | =cut | ||||||||||
| 303 | |||||||||||
| 304 | 1 | has mother => ( | |||||||||
| 305 | is => 'lazy', | ||||||||||
| 306 | ); | ||||||||||
| 307 | |||||||||||
| 308 | 4 | ▶ | sub _build_mother { | ||||||||
|
Subroutine
_build_mother: called CC=1 SCAR=0.0
| |||||||||||
| 309 | 4 | return ahnen($_[0]->ahnentafel * 2 + 1); | |||||||||
| 310 | } | ||||||||||
| 311 | |||||||||||
| 312 | =head2 first_in_generation | ||||||||||
| 313 | |||||||||||
| 314 | The lowest Ahnentafel number that appears in the current generation. | ||||||||||
| 315 | |||||||||||
| 316 | =cut | ||||||||||
| 317 | |||||||||||
| 318 | 1 | has first_in_generation => ( | |||||||||
| 319 | is => 'lazy', | ||||||||||
| 320 | isa => Int, | ||||||||||
| 321 | ); | ||||||||||
| 322 | |||||||||||
| 323 | 3 | ▶ | sub _build_first_in_generation { | ||||||||
|
Subroutine
_build_first_in_generation: called CC=1 SCAR=0.0
| |||||||||||
| 324 | 3 | return 2 ** ($_[0]->generation - 1); | |||||||||
| 325 | } | ||||||||||
| 326 | |||||||||||
| 327 | =head2 is_first_in_generation | ||||||||||
| 328 | |||||||||||
| 329 | Is this the first Ahnentafel number in the current generation? | ||||||||||
| 330 | |||||||||||
| 331 | =cut | ||||||||||
| 332 | |||||||||||
| 333 | 1 | has is_first_in_generation => ( | |||||||||
| 334 | is => 'lazy', | ||||||||||
| 335 | isa => Bool, | ||||||||||
| 336 | ); | ||||||||||
| 337 | |||||||||||
| 338 | 3 | ▶ | sub _build_is_first_in_generation { | ||||||||
|
Subroutine
_build_is_first_in_generation: called CC=1 SCAR=0.0
| |||||||||||
| 339 | 3 | return $_[0]->first_in_generation == $_[0]->ahnentafel; | |||||||||
| 340 | } | ||||||||||
| 341 | |||||||||||
| 342 | =head2 last_in_generation | ||||||||||
| 343 | |||||||||||
| 344 | The highest Ahnentafel number that appears in the current generation. | ||||||||||
| 345 | |||||||||||
| 346 | =cut | ||||||||||
| 347 | |||||||||||
| 348 | 1 | has last_in_generation => ( | |||||||||
| 349 | is => 'lazy', | ||||||||||
| 350 | isa => Int, | ||||||||||
| 351 | ); | ||||||||||
| 352 | |||||||||||
| 353 | 3 | ▶ | sub _build_last_in_generation { | ||||||||
|
Subroutine
_build_last_in_generation: called CC=1 SCAR=0.0
| |||||||||||
| 354 | 3 | return 2 ** $_[0]->generation - 1; | |||||||||
| 355 | } | ||||||||||
| 356 | |||||||||||
| 357 | =head2 is_last_in_generation | ||||||||||
| 358 | |||||||||||
| 359 | Is this the last Ahnentafel number in the current generation? | ||||||||||
| 360 | |||||||||||
| 361 | =cut | ||||||||||
| 362 | |||||||||||
| 363 | 1 | has is_last_in_generation => ( | |||||||||
| 364 | is => 'lazy', | ||||||||||
| 365 | isa => Bool, | ||||||||||
| 366 | ); | ||||||||||
| 367 | |||||||||||
| 368 | 3 | ▶ | sub _build_is_last_in_generation { | ||||||||
|
Subroutine
_build_is_last_in_generation: called CC=1 SCAR=0.0
| |||||||||||
| 369 | 3 | return $_[0]->last_in_generation == $_[0]->ahnentafel; | |||||||||
| 370 | } | ||||||||||
| 371 | |||||||||||
| 372 | =head1 AUTHOR | ||||||||||
| 373 | |||||||||||
| 374 | Dave Cross <dave@perlhacks.com> | ||||||||||
| 375 | |||||||||||
| 376 | =head1 COPYRIGHT AND LICENCE | ||||||||||
| 377 | |||||||||||
| 378 | Copyright (c) 2016, Magnum Solutions Ltd. All Rights Reserved. | ||||||||||
| 379 | |||||||||||
| 380 | This library is free software; you can redistribute it and/or modify it | ||||||||||
| 381 | under the same terms as Perl itself. | ||||||||||
| 382 | |||||||||||
| 383 | =cut | ||||||||||
| 384 | |||||||||||
| 385 | 1 | 1; | |||||||||