| 1 | package Genealogy::Relationship::Name; | ||
| 2 | |||
| 3 | # Genealogy::Relationship::Name - Return the name of a genealogical relationship | ||
| 4 | # given step counts to and from a common ancestor, the sex of person B, and a language. | ||
| 5 | # | ||
| 6 | # Author: Nigel Horne <njh@nigelhorne.com> | ||
| 7 | # Licence: GPL v2 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use autodie qw(:all); | ||
| 12 | |||
| 13 | use Carp qw(croak carp); | ||
| 14 | use Object::Configure; | ||
| 15 | use Params::Get; | ||
| 16 | use Params::Validate::Strict 0.31; | ||
| 17 | use Readonly; | ||
| 18 | |||
| 19 | our $VERSION = '0.02'; | ||
| 20 | |||
| 21 | # --------------------------------------------------------------------------- | ||
| 22 | # Constants – relationship table keys | ||
| 23 | # --------------------------------------------------------------------------- | ||
| 24 | |||
| 25 | # Supported language codes | ||
| 26 | Readonly::Array my @SUPPORTED_LANGUAGES => qw(en fr de); | ||
| 27 | |||
| 28 | # Default language when none supplied | ||
| 29 | Readonly::Scalar my $DEFAULT_LANGUAGE => 'en'; | ||
| 30 | |||
| 31 | # Sex constants | ||
| 32 | Readonly::Scalar my $SEX_MALE => 'M'; | ||
| 33 | Readonly::Scalar my $SEX_FEMALE => 'F'; | ||
| 34 | |||
| 35 | # --------------------------------------------------------------------------- | ||
| 36 | # English relationship tables | ||
| 37 | # Key: "$steps1,$steps2" where steps1 = steps from A to ancestor, | ||
| 38 | # steps2 = steps from B to ancestor | ||
| 39 | # --------------------------------------------------------------------------- | ||
| 40 | |||
| 41 | Readonly::Hash my %EN_MALE_RELATIONSHIPS => ( | ||
| 42 | '0,0' => 'self', | ||
| 43 | '0,1' => 'son', | ||
| 44 | '0,2' => 'grandson', | ||
| 45 | '0,3' => 'great-grandson', | ||
| 46 | '0,4' => 'great-great-grandson', | ||
| 47 | '1,0' => 'father', | ||
| 48 | '1,1' => 'brother', | ||
| 49 | '1,2' => 'nephew', | ||
| 50 | '1,3' => 'great-nephew', | ||
| 51 | '1,4' => 'great-great-nephew', | ||
| 52 | '2,0' => 'grandfather', | ||
| 53 | '2,1' => 'uncle', | ||
| 54 | '2,2' => 'first cousin', | ||
| 55 | '2,3' => 'first cousin once-removed', | ||
| 56 | '2,4' => 'first cousin twice-removed', | ||
| 57 | '2,5' => 'first cousin three-times-removed', | ||
| 58 | '3,0' => 'great-grandfather', | ||
| 59 | '3,1' => 'great-uncle', | ||
| 60 | '3,2' => 'first cousin once-removed', | ||
| 61 | '3,3' => 'second cousin', | ||
| 62 | '3,4' => 'second cousin once-removed', | ||
| 63 | '3,5' => 'second cousin twice-removed', | ||
| 64 | '3,6' => 'second cousin three-times-removed', | ||
| 65 | '4,0' => 'great-great-grandfather', | ||
| 66 | '4,1' => 'great-great-uncle', | ||
| 67 | '4,2' => 'first cousin twice-removed', | ||
| 68 | '4,3' => 'second cousin once-removed', | ||
| 69 | '4,4' => 'third cousin', | ||
| 70 | '4,5' => 'third cousin once-removed', | ||
| 71 | '4,6' => 'third cousin twice-removed', | ||
| 72 | '5,0' => 'great-great-great-grandfather', | ||
| 73 | '5,1' => 'great-great-great-uncle', | ||
| 74 | '5,2' => 'first cousin three-times-removed', | ||
| 75 | '5,3' => 'second cousin twice-removed', | ||
| 76 | '5,4' => 'third cousin once-removed', | ||
| 77 | '5,5' => 'fourth cousin', | ||
| 78 | '5,6' => 'fourth cousin once-removed', | ||
| 79 | '6,0' => 'great-great-great-great-grandfather', | ||
| 80 | '6,2' => 'first cousin four-times-removed', | ||
| 81 | '6,3' => 'second cousin three-times-removed', | ||
| 82 | '6,4' => 'third cousin twice-removed', | ||
| 83 | '6,5' => 'fourth cousin once-removed', | ||
| 84 | '6,6' => 'fifth cousin', | ||
| 85 | ); | ||
| 86 | |||
| 87 | Readonly::Hash my %EN_FEMALE_RELATIONSHIPS => ( | ||
| 88 | '0,0' => 'self', | ||
| 89 | '0,1' => 'daughter', | ||
| 90 | '0,2' => 'granddaughter', | ||
| 91 | '0,3' => 'great-granddaughter', | ||
| 92 | '0,4' => 'great-great-granddaughter', | ||
| 93 | '1,0' => 'mother', | ||
| 94 | '1,1' => 'sister', | ||
| 95 | '1,2' => 'niece', | ||
| 96 | '1,3' => 'great-niece', | ||
| 97 | '1,4' => 'great-great-niece', | ||
| 98 | '2,0' => 'grandmother', | ||
| 99 | '2,1' => 'aunt', | ||
| 100 | '2,2' => 'first cousin', | ||
| 101 | '2,3' => 'first cousin once-removed', | ||
| 102 | '2,4' => 'first cousin twice-removed', | ||
| 103 | '2,5' => 'first cousin three-times-removed', | ||
| 104 | '3,0' => 'great-grandmother', | ||
| 105 | '3,1' => 'great-aunt', | ||
| 106 | '3,2' => 'first cousin once-removed', | ||
| 107 | '3,3' => 'second cousin', | ||
| 108 | '3,4' => 'second cousin once-removed', | ||
| 109 | '3,5' => 'second cousin twice-removed', | ||
| 110 | '3,6' => 'second cousin three-times-removed', | ||
| 111 | '4,0' => 'great-great-grandmother', | ||
| 112 | '4,1' => 'great-great-aunt', | ||
| 113 | '4,2' => 'first cousin twice-removed', | ||
| 114 | '4,3' => 'second cousin once-removed', | ||
| 115 | '4,4' => 'third cousin', | ||
| 116 | '4,5' => 'third cousin once-removed', | ||
| 117 | '4,6' => 'third cousin twice-removed', | ||
| 118 | '5,0' => 'great-great-great-grandmother', | ||
| 119 | '5,1' => 'great-great-great-aunt', | ||
| 120 | '5,2' => 'first cousin three-times-removed', | ||
| 121 | '5,3' => 'second cousin twice-removed', | ||
| 122 | '5,4' => 'third cousin once-removed', | ||
| 123 | '5,5' => 'fourth cousin', | ||
| 124 | '5,6' => 'fourth cousin once-removed', | ||
| 125 | '6,0' => 'great-great-great-great-grandmother', | ||
| 126 | '6,2' => 'first cousin four-times-removed', | ||
| 127 | '6,3' => 'second cousin three-times-removed', | ||
| 128 | '6,4' => 'third cousin twice-removed', | ||
| 129 | '6,5' => 'fourth cousin once-removed', | ||
| 130 | '6,6' => 'fifth cousin', | ||
| 131 | ); | ||
| 132 | |||
| 133 | # --------------------------------------------------------------------------- | ||
| 134 | # French relationship tables | ||
| 135 | # --------------------------------------------------------------------------- | ||
| 136 | |||
| 137 | Readonly::Hash my %FR_MALE_RELATIONSHIPS => ( | ||
| 138 | '0,0' => 'soi-meme', | ||
| 139 | '0,1' => 'fils', | ||
| 140 | '0,2' => 'petit-fils', | ||
| 141 | '0,3' => 'arriere-petit-fils', | ||
| 142 | '0,4' => 'arriere-arriere-petit-fils', | ||
| 143 | '1,0' => 'pere', | ||
| 144 | '1,1' => 'frere', | ||
| 145 | '1,2' => 'neveu', | ||
| 146 | '1,3' => 'grand-neveu', | ||
| 147 | '1,4' => 'arriere-grand-neveu', | ||
| 148 | '2,0' => 'grand-pere', | ||
| 149 | '2,1' => 'oncle', | ||
| 150 | '2,2' => 'cousin germain', | ||
| 151 | '2,3' => 'cousin issu de germain', | ||
| 152 | '2,4' => 'cousin au troisieme degre', | ||
| 153 | '3,0' => 'arriere-grand-pere', | ||
| 154 | '3,1' => 'grand-oncle', | ||
| 155 | '3,2' => 'cousin issu de germain', | ||
| 156 | '3,3' => 'cousin au troisieme degre', | ||
| 157 | '3,4' => 'cousin au quatrieme degre', | ||
| 158 | '4,0' => 'arriere-arriere-grand-pere', | ||
| 159 | '4,1' => 'arriere-grand-oncle', | ||
| 160 | '4,2' => 'cousin au troisieme degre', | ||
| 161 | '4,3' => 'cousin au quatrieme degre', | ||
| 162 | '4,4' => 'cousin au cinquieme degre', | ||
| 163 | '5,0' => 'trisaieul', | ||
| 164 | '5,5' => 'cousin au sixieme degre', | ||
| 165 | '6,0' => 'quadrisaieul', | ||
| 166 | '6,6' => 'cousin au septieme degre', | ||
| 167 | ); | ||
| 168 | |||
| 169 | Readonly::Hash my %FR_FEMALE_RELATIONSHIPS => ( | ||
| 170 | '0,0' => 'soi-meme', | ||
| 171 | '0,1' => 'fille', | ||
| 172 | '0,2' => 'petite-fille', | ||
| 173 | '0,3' => 'arriere-petite-fille', | ||
| 174 | '0,4' => 'arriere-arriere-petite-fille', | ||
| 175 | '1,0' => 'mere', | ||
| 176 | '1,1' => 'soeur', | ||
| 177 | '1,2' => 'niece', | ||
| 178 | '1,3' => 'grand-niece', | ||
| 179 | '1,4' => 'arriere-grand-niece', | ||
| 180 | '2,0' => 'grand-mere', | ||
| 181 | '2,1' => 'tante', | ||
| 182 | '2,2' => 'cousine germaine', | ||
| 183 | '2,3' => 'cousine issue de germaine', | ||
| 184 | '2,4' => 'cousine au troisieme degre', | ||
| 185 | '3,0' => 'arriere-grand-mere', | ||
| 186 | '3,1' => 'grand-tante', | ||
| 187 | '3,2' => 'cousine issue de germaine', | ||
| 188 | '3,3' => 'cousine au troisieme degre', | ||
| 189 | '3,4' => 'cousine au quatrieme degre', | ||
| 190 | '4,0' => 'arriere-arriere-grand-mere', | ||
| 191 | '4,1' => 'arriere-grand-tante', | ||
| 192 | '4,2' => 'cousine au troisieme degre', | ||
| 193 | '4,3' => 'cousine au quatrieme degre', | ||
| 194 | '4,4' => 'cousine au cinquieme degre', | ||
| 195 | '5,0' => 'trisaieule', | ||
| 196 | '5,5' => 'cousine au sixieme degre', | ||
| 197 | '6,0' => 'quadrisaieule', | ||
| 198 | '6,6' => 'cousine au septieme degre', | ||
| 199 | ); | ||
| 200 | |||
| 201 | # --------------------------------------------------------------------------- | ||
| 202 | # German relationship tables | ||
| 203 | # --------------------------------------------------------------------------- | ||
| 204 | |||
| 205 | Readonly::Hash my %DE_MALE_RELATIONSHIPS => ( | ||
| 206 | '0,0' => 'sich selbst', | ||
| 207 | '0,1' => 'Sohn', | ||
| 208 | '0,2' => 'Enkel', | ||
| 209 | '0,3' => 'Urenkel', | ||
| 210 | '0,4' => 'Ururenkel', | ||
| 211 | '1,0' => 'Vater', | ||
| 212 | '1,1' => 'Bruder', | ||
| 213 | '1,2' => 'Neffe', | ||
| 214 | '1,3' => 'Grossneffe', | ||
| 215 | '1,4' => 'Urgrossneffe', | ||
| 216 | '2,0' => 'Grossvater', | ||
| 217 | '2,1' => 'Onkel', | ||
| 218 | '2,2' => 'Cousin', | ||
| 219 | '2,3' => 'Cousin ersten Grades einmal entfernt', | ||
| 220 | '2,4' => 'Cousin ersten Grades zweimal entfernt', | ||
| 221 | '3,0' => 'Urgrossvater', | ||
| 222 | '3,1' => 'Grossonkel', | ||
| 223 | '3,2' => 'Cousin ersten Grades einmal entfernt', | ||
| 224 | '3,3' => 'Cousin zweiten Grades', | ||
| 225 | '3,4' => 'Cousin zweiten Grades einmal entfernt', | ||
| 226 | '4,0' => 'Urururgrossvater', | ||
| 227 | '4,1' => 'Urgrossonkel', | ||
| 228 | '4,2' => 'Cousin ersten Grades zweimal entfernt', | ||
| 229 | '4,3' => 'Cousin zweiten Grades einmal entfernt', | ||
| 230 | '4,4' => 'Cousin dritten Grades', | ||
| 231 | '5,0' => 'Urururgrossvater', | ||
| 232 | '5,5' => 'Cousin vierten Grades', | ||
| 233 | '6,0' => 'Ururururgrossvater', | ||
| 234 | '6,6' => 'Cousin fuenften Grades', | ||
| 235 | ); | ||
| 236 | |||
| 237 | Readonly::Hash my %DE_FEMALE_RELATIONSHIPS => ( | ||
| 238 | '0,0' => 'sich selbst', | ||
| 239 | '0,1' => 'Tochter', | ||
| 240 | '0,2' => 'Enkelin', | ||
| 241 | '0,3' => 'Urenkelin', | ||
| 242 | '0,4' => 'Ururenkelin', | ||
| 243 | '1,0' => 'Mutter', | ||
| 244 | '1,1' => 'Schwester', | ||
| 245 | '1,2' => 'Nichte', | ||
| 246 | '1,3' => 'Grossnichte', | ||
| 247 | '1,4' => 'Urgrossnichte', | ||
| 248 | '2,0' => 'Grossmutter', | ||
| 249 | '2,1' => 'Tante', | ||
| 250 | '2,2' => 'Cousine', | ||
| 251 | '2,3' => 'Cousine ersten Grades einmal entfernt', | ||
| 252 | '2,4' => 'Cousine ersten Grades zweimal entfernt', | ||
| 253 | '3,0' => 'Urgrossmutter', | ||
| 254 | '3,1' => 'Grosstante', | ||
| 255 | '3,2' => 'Cousine ersten Grades einmal entfernt', | ||
| 256 | '3,3' => 'Cousine zweiten Grades', | ||
| 257 | '3,4' => 'Cousine zweiten Grades einmal entfernt', | ||
| 258 | '4,0' => 'Urururgrossmutter', | ||
| 259 | '4,1' => 'Urgrosstante', | ||
| 260 | '4,2' => 'Cousine ersten Grades zweimal entfernt', | ||
| 261 | '4,3' => 'Cousine zweiten Grades einmal entfernt', | ||
| 262 | '4,4' => 'Cousine dritten Grades', | ||
| 263 | '5,0' => 'Urururgrossmutter', | ||
| 264 | '5,5' => 'Cousine vierten Grades', | ||
| 265 | '6,0' => 'Ururururgrossmutter', | ||
| 266 | '6,6' => 'Cousine fuenften Grades', | ||
| 267 | ); | ||
| 268 | |||
| 269 | # --------------------------------------------------------------------------- | ||
| 270 | # Master dispatch table: lang -> sex -> hashref | ||
| 271 | # --------------------------------------------------------------------------- | ||
| 272 | |||
| 273 | Readonly::Hash my %RELATIONSHIP_TABLES => ( | ||
| 274 | 'en' => { | ||
| 275 | $SEX_MALE => \%EN_MALE_RELATIONSHIPS, | ||
| 276 | $SEX_FEMALE => \%EN_FEMALE_RELATIONSHIPS, | ||
| 277 | }, 'fr' => { | ||
| 278 | $SEX_MALE => \%FR_MALE_RELATIONSHIPS, | ||
| 279 | $SEX_FEMALE => \%FR_FEMALE_RELATIONSHIPS, | ||
| 280 | }, 'de' => { | ||
| 281 | $SEX_MALE => \%DE_MALE_RELATIONSHIPS, | ||
| 282 | $SEX_FEMALE => \%DE_FEMALE_RELATIONSHIPS, | ||
| 283 | }, | ||
| 284 | ); | ||
| 285 | |||
| 286 | # --------------------------------------------------------------------------- | ||
| 287 | # Constructor | ||
| 288 | # --------------------------------------------------------------------------- | ||
| 289 | |||
| 290 | =head1 NAME | ||
| 291 | |||
| 292 | Genealogy::Relationship::Name - Return a genealogical relationship name from step counts | ||
| 293 | |||
| 294 | =head1 VERSION | ||
| 295 | |||
| 296 | Version 0.02 | ||
| 297 | |||
| 298 | =head1 SYNOPSIS | ||
| 299 | |||
| 300 | use Genealogy::Relationship::Name; | ||
| 301 | |||
| 302 | my $namer = Genealogy::Relationship::Name->new(); | ||
| 303 | |||
| 304 | my $name = $namer->name( | ||
| 305 | steps_to_ancestor => 2, | ||
| 306 | steps_from_ancestor => 3, | ||
| 307 | sex => 'F', | ||
| 308 | ); | ||
| 309 | # Returns 'first cousin once-removed' | ||
| 310 | |||
| 311 | # With language | ||
| 312 | my $name_fr = $namer->name( | ||
| 313 | steps_to_ancestor => 2, | ||
| 314 | steps_from_ancestor => 2, | ||
| 315 | sex => 'M', | ||
| 316 | language => 'fr', | ||
| 317 | ); | ||
| 318 | # Returns 'cousin germain' | ||
| 319 | |||
| 320 | =head1 DESCRIPTION | ||
| 321 | |||
| 322 | C<Genealogy::Relationship::Name> maps a pair of step-counts (person A to common | ||
| 323 | ancestor, common ancestor to person B) plus the sex of person B and an optional | ||
| 324 | language code to a human-readable relationship name string. | ||
| 325 | |||
| 326 | The relationship tables were originally embedded in the C<gedcom> and C<ged2site> | ||
| 327 | distributions inside C<Gedcom::Individual::relationship_up()>; this module | ||
| 328 | extracts them into a reusable, installable CPAN distribution. | ||
| 329 | |||
| 330 | Supported languages: C<en> (English, default), C<fr> (French), C<de> (German). | ||
| 331 | |||
| 332 | =head1 METHODS | ||
| 333 | |||
| 334 | =head2 new | ||
| 335 | |||
| 336 | Constructor. Creates and returns a blessed C<Genealogy::Relationship::Name> | ||
| 337 | object. | ||
| 338 | |||
| 339 | =head3 PURPOSE | ||
| 340 | |||
| 341 | Initialises the object with optional configuration: a default language for | ||
| 342 | subsequent C<name()> calls, and an optional L<Log::Abstraction> object to | ||
| 343 | use as the error logger. Configuration may also be loaded from an INI-style | ||
| 344 | file via L<Object::Configure>. | ||
| 345 | |||
| 346 | =head3 ARGUMENTS | ||
| 347 | |||
| 348 | =over 4 | ||
| 349 | |||
| 350 | =item C<language> (string, optional) | ||
| 351 | |||
| 352 | Default BCP-47 language tag (primary subtag only) for all C<name()> calls | ||
| 353 | on this object. Supported values: C<en> (default), C<fr>, C<de>. May be | ||
| 354 | overridden per-call by passing C<language> to C<name()>. | ||
| 355 | |||
| 356 | =item C<logger> | ||
| 357 | |||
| 358 | A pre-constructed loggining object. When a required argument is | ||
| 359 | passed as C<undef> to C<name()>, the error is reported via | ||
| 360 | C<< $logger->error($msg) >> rather than C<croak>. This allows | ||
| 361 | programs to route errors through their own | ||
| 362 | infrastructure with full C<ctx> context. | ||
| 363 | |||
| 364 | See L<Log::Abstraction> and the L</CONFIGURATION> section for the | ||
| 365 | recommended construction pattern. | ||
| 366 | |||
| 367 | =item C<config_file> (string, optional) | ||
| 368 | |||
| 369 | Path to an INI-style configuration file processed by L<Object::Configure>. | ||
| 370 | Any keys it sets may be overridden by arguments passed directly to C<new()>. | ||
| 371 | |||
| 372 | =back | ||
| 373 | |||
| 374 | =head3 RETURNS | ||
| 375 | |||
| 376 | A blessed C<Genealogy::Relationship::Name> object. | ||
| 377 | |||
| 378 | =head3 SIDE EFFECTS | ||
| 379 | |||
| 380 | Calls L<Object::Configure> C<configure()>, which may read from a | ||
| 381 | configuration file on disk if C<config_file> is supplied or if a default | ||
| 382 | configuration file exists for the class. | ||
| 383 | |||
| 384 | =head3 NOTES | ||
| 385 | |||
| 386 | L<Object::Configure> cannot handle object or coderef values (it treats | ||
| 387 | unknown scalar values as configuration file paths). The C<logger> key is | ||
| 388 | therefore stashed before the C<configure()> call and restored afterward. | ||
| 389 | Any future object-valued constructor arguments must follow the same pattern. | ||
| 390 | |||
| 391 | =head3 EXAMPLE | ||
| 392 | |||
| 393 | use Genealogy::Relationship::Name; | ||
| 394 | use Log::Abstraction; | ||
| 395 | |||
| 396 | # Minimal construction | ||
| 397 | my $namer = Genealogy::Relationship::Name->new(); | ||
| 398 | |||
| 399 | # With a default language | ||
| 400 | my $namer_fr = Genealogy::Relationship::Name->new(language => 'fr'); | ||
| 401 | |||
| 402 | # With a Log::Abstraction logger | ||
| 403 | my $la = Log::Abstraction->new( | ||
| 404 | logger => sub { | ||
| 405 | my $args = shift; | ||
| 406 | my $msg = $args->{ctx} | ||
| 407 | ? $args->{ctx}->as_string() . ': ' . join('', @{$args->{message}}) | ||
| 408 | : join('', @{$args->{message}}); | ||
| 409 | complain({ message => $msg, person => $args->{ctx} }); | ||
| 410 | }, | ||
| 411 | ctx => $individual, | ||
| 412 | ); | ||
| 413 | my $namer = Genealogy::Relationship::Name->new(language => 'en', logger => $la); | ||
| 414 | |||
| 415 | =head3 API SPECIFICATION | ||
| 416 | |||
| 417 | =head4 Input | ||
| 418 | |||
| 419 | { | ||
| 420 | language => { type => 'string', regex => qr/^(?:en|fr|de)/, optional => 1 }, | ||
| 421 | logger => { type => 'object', optional => 1 }, | ||
| 422 | } | ||
| 423 | |||
| 424 | =head4 Output | ||
| 425 | |||
| 426 | { | ||
| 427 | type => 'object', | ||
| 428 | class => 'Genealogy::Relationship::Name', | ||
| 429 | } | ||
| 430 | |||
| 431 | =head3 FORMAL SPECIFICATION | ||
| 432 | |||
| 433 | new ________________________________________________________ | ||
| 434 | [In] class : String (class name or object) | ||
| 435 | language : {en, fr, de}? (optional default language) | ||
| 436 | logger : Log::Abstraction? (optional error logger) | ||
| 437 | [Out] self : Genealogy::Relationship::Name | ||
| 438 | |||
| 439 | Let params == get_params(args) | ||
| 440 | Let params' == configure(class, params \ {logger}) | ||
| 441 | union {logger -> params.logger} if logger in dom params | ||
| 442 | self == bless(params', class) | ||
| 443 | |||
| 444 | post: self.language == params.language if language in dom params | ||
| 445 | self.logger == params.logger if logger in dom params | ||
| 446 | ref(self) == 'Genealogy::Relationship::Name' | ||
| 447 | |||
| 448 | =cut | ||
| 449 | |||
| 450 | sub new { | ||
| 451 | # Create and return a blessed object | ||
| 452 | my $class = shift; | ||
| 453 | |||
| 454 | # Handle hash or hashref arguments | ||
| 455 | my $params = Params::Get::get_params(undef, \@_); | ||
| 456 | |||
| 457 | # Stash keys that are coderefs or objects before passing to Object::Configure, | ||
| 458 | # which cannot handle them (it treats unknown scalar values as config file paths) | ||
| 459 | my %stash; | ||
| 460 | for my $key (qw(logger)) { | ||
| 461 | next unless exists $params->{$key}; | ||
| 462 | $stash{$key} = delete $params->{$key}; | ||
| 463 | } | ||
| 464 | |||
| 465 | # Load the configuration from a config file, if provided | ||
| 466 | $params = Object::Configure::configure($class, $params); | ||
| 467 | |||
| 468 | # Restore the stashed coderefs and objects after configure() | ||
| 469 | @{$params}{keys %stash} = values %stash; | ||
| 470 | |||
| 471 | # Bless and return the object; logger/ctx/level keys from params | ||
| 472 | # are stored directly and accessed via $self->{logger} etc. | ||
| 473 | return bless { | ||
| 474 | # Store any constructor-time config for Object::Configure compatibility | ||
| 475 | %{$params}, | ||
| 476 | }, ref($class) || $class; | ||
| 477 | } | ||
| 478 | |||
| 479 | # --------------------------------------------------------------------------- | ||
| 480 | # Public method: name() | ||
| 481 | # --------------------------------------------------------------------------- | ||
| 482 | |||
| 483 | =head1 METHODS | ||
| 484 | |||
| 485 | =head2 name | ||
| 486 | |||
| 487 | Returns the name of the relationship between person A and person B. | ||
| 488 | |||
| 489 | =head3 PURPOSE | ||
| 490 | |||
| 491 | Given the number of steps from person A up to the nearest common ancestor | ||
| 492 | (C<steps_to_ancestor>) and the number of steps from that ancestor down to | ||
| 493 | person B (C<steps_from_ancestor>), plus the sex of person B and a language | ||
| 494 | code, returns a localised relationship-name string. | ||
| 495 | |||
| 496 | =head3 ARGUMENTS | ||
| 497 | |||
| 498 | =over 4 | ||
| 499 | |||
| 500 | =item C<steps_to_ancestor> (integer, required) | ||
| 501 | |||
| 502 | Number of generational steps from person A up to the common ancestor. | ||
| 503 | Must be a non-negative integer. Zero means person A I<is> the ancestor. | ||
| 504 | |||
| 505 | =item C<steps_from_ancestor> (integer, required) | ||
| 506 | |||
| 507 | Number of generational steps from the common ancestor down to person B. | ||
| 508 | Must be a non-negative integer. | ||
| 509 | |||
| 510 | =item C<sex> (string, required) | ||
| 511 | |||
| 512 | Sex of person B. Must be C<'M'> (male) or C<'F'> (female). | ||
| 513 | |||
| 514 | =item C<language> (string, optional) | ||
| 515 | |||
| 516 | BCP-47-style language tag (only the primary subtag is used). | ||
| 517 | Supported values: C<en> (default), C<fr>, C<de>. | ||
| 518 | |||
| 519 | =item C<person> (object, optional) | ||
| 520 | |||
| 521 | An optional person object (e.g. a C<Gedcom::Individual> instance) passed | ||
| 522 | through to the error handler when an error occurs. Takes priority over the | ||
| 523 | C<ctx> set at construction time. The handler receives it as C<ctx> (logger | ||
| 524 | path) or C<person> (on_error path), matching the C<complain()> interface | ||
| 525 | in C<gedcom>/C<ged2site>. | ||
| 526 | |||
| 527 | =back | ||
| 528 | |||
| 529 | =head3 RETURNS | ||
| 530 | |||
| 531 | A string containing the relationship name, or C<undef> if the combination | ||
| 532 | is not found in the lookup table. | ||
| 533 | |||
| 534 | =head3 EXAMPLE | ||
| 535 | |||
| 536 | my $namer = Genealogy::Relationship::Name->new(); | ||
| 537 | |||
| 538 | # Person A is the grandparent (2 steps up) of the common ancestor, | ||
| 539 | # and person B is 3 steps below the ancestor; B is female => first cousin once-removed | ||
| 540 | my $rel = $namer->name( | ||
| 541 | steps_to_ancestor => 2, | ||
| 542 | steps_from_ancestor => 3, | ||
| 543 | sex => 'F', | ||
| 544 | ); | ||
| 545 | |||
| 546 | =head3 API SPECIFICATION | ||
| 547 | |||
| 548 | =head4 Input | ||
| 549 | |||
| 550 | { | ||
| 551 | steps_to_ancestor => { type => 'integer', minimum => 0 }, | ||
| 552 | steps_from_ancestor => { type => 'integer', minimum => 0 }, | ||
| 553 | sex => { type => 'string', memberof => ['M', 'F'] }, | ||
| 554 | language => { type => 'string', regex => qr/^(?:en|de|fr)/, optional => 1 }, | ||
| 555 | # person is extracted before validate_strict to avoid PVS inferring constraints | ||
| 556 | } | ||
| 557 | |||
| 558 | =head4 Output | ||
| 559 | |||
| 560 | { | ||
| 561 | type => 'string', | ||
| 562 | optional => 1, # undef when the combination is not tabulated | ||
| 563 | } | ||
| 564 | |||
| 565 | =head3 FORMAL SPECIFICATION | ||
| 566 | |||
| 567 | name ______________________________________________________ | ||
| 568 | [In] steps_to_ancestor : N0 | ||
| 569 | steps_from_ancestor : N0 | ||
| 570 | sex : {M, F} | ||
| 571 | language : {en, fr, de}? (default en) | ||
| 572 | person : Object? | ||
| 573 | [Out] result : String | undef | ||
| 574 | |||
| 575 | Let key == steps_to_ancestor ++ "," ++ steps_from_ancestor | ||
| 576 | Let table == RELATIONSHIP_TABLES(language)(sex) | ||
| 577 | result == table(key) if key in dom table | ||
| 578 | == undef otherwise | ||
| 579 | |||
| 580 | =cut | ||
| 581 | |||
| 582 | sub name { | ||
| 583 | my $self = shift; | ||
| 584 | |||
| 585 | # Validate and extract the remaining parameters; capture the return value | ||
| 586 | my $args = Params::Validate::Strict::validate_strict( | ||
| 587 | args => Params::Get::get_params(undef, \@_) || {}, | ||
| 588 | schema => { | ||
| 589 | steps_to_ancestor => { type => 'integer', minimum => 0 }, | ||
| 590 | steps_from_ancestor => { type => 'integer', minimum => 0 }, | ||
| 591 | sex => { type => 'string', memberof => ['M', 'F'] }, | ||
| 592 | language => { type => 'string', regex => qr/^(?:en|de|fr)/, | ||
| 593 | optional => 1 }, | ||
| 594 | person => { type => 'object', optional => 1 }, | ||
| 595 | } | ||
| 596 | ); | ||
| 597 | |||
| 598 | # Extract individual parameters; undef means arg was not given, so croak | ||
| 599 | foreach my $arg('steps_to_ancestor', 'steps_from_ancestor', 'sex') { | ||
| 600 | if(!defined($args->{$arg})) { | ||
| 601 | if(my $logger = $self->{logger}) { | ||
| 602 | $logger->error("$arg not given"); | ||
| 603 | } else { | ||
| 604 | croak("$arg not given"); | ||
| 605 | } | ||
| 606 | } | ||
| 607 | } | ||
| 608 | |||
| 609 | my $steps1 = $args->{steps_to_ancestor} // croak('steps_to_ancestor not given'); | ||
| 610 | my $steps2 = $args->{steps_from_ancestor} // croak('steps_from_ancestor not given'); | ||
| 611 | my $sex = $args->{sex} // croak('sex not given'); | ||
| 612 | my $person = $args->{person}; | ||
| 613 | |||
| 614 | # Fall back to the constructor default or hard default if no per-call language given | ||
| 615 | my $lang = lc($args->{language} // $self->{language} // $DEFAULT_LANGUAGE); | ||
| 616 | |||
| 617 | # Strip any region subtag (e.g. 'en-GB' -> 'en') after lowercasing | ||
| 618 | ($lang) = split /-/, $lang; | ||
| 619 | |||
| 620 | # Build lookup key from the two-step counts | ||
| 621 | my $key = "${steps1},${steps2}"; | ||
| 622 | |||
| 623 | # Retrieve the correct gender-specific table for the chosen language | ||
| 624 | my $table = $RELATIONSHIP_TABLES{$lang}{$sex}; | ||
| 625 | |||
| 626 | # Look up and return; undef when the combination is not tabulated | ||
| 627 | my $result = $table->{$key}; | ||
| 628 | |||
| 629 | return $result; | ||
| 630 | } | ||
| 631 | |||
| 632 | # --------------------------------------------------------------------------- | ||
| 633 | # Public method: supported_languages() | ||
| 634 | # --------------------------------------------------------------------------- | ||
| 635 | |||
| 636 | =head2 supported_languages | ||
| 637 | |||
| 638 | Returns a sorted list of the language codes that the module supports. | ||
| 639 | |||
| 640 | =head3 PURPOSE | ||
| 641 | |||
| 642 | Allows calling code to enumerate the languages available for C<name()> | ||
| 643 | without hard-coding them. | ||
| 644 | |||
| 645 | =head3 ARGUMENTS | ||
| 646 | |||
| 647 | None. | ||
| 648 | |||
| 649 | =head3 RETURNS | ||
| 650 | |||
| 651 | A list (or array-ref in scalar context) of language code strings, | ||
| 652 | currently C<('de', 'en', 'fr')>. | ||
| 653 | |||
| 654 | =head3 EXAMPLE | ||
| 655 | |||
| 656 | my @langs = $namer->supported_languages(); | ||
| 657 | # ( 'de', 'en', 'fr' ) | ||
| 658 | |||
| 659 | =head3 API SPECIFICATION | ||
| 660 | |||
| 661 | =head4 Input | ||
| 662 | |||
| 663 | {} # no arguments | ||
| 664 | |||
| 665 | =head4 Output | ||
| 666 | |||
| 667 | { | ||
| 668 | type => ARRAYREF, # sorted list of language codes | ||
| 669 | } | ||
| 670 | |||
| 671 | =head3 FORMAL SPECIFICATION | ||
| 672 | |||
| 673 | supported_languages ______________________________________ | ||
| 674 | [In] (none) | ||
| 675 | [Out] result : seq String | ||
| 676 | |||
| 677 | result == sort(dom RELATIONSHIP_TABLES) | ||
| 678 | |||
| 679 | =cut | ||
| 680 | |||
| 681 | sub supported_languages { | ||
| 682 | # Return the sorted set of keys from the master dispatch table | ||
| 683 | my @langs = sort keys %RELATIONSHIP_TABLES; | ||
| 684 | return wantarray ? @langs : \@langs; | ||
| 685 | } | ||
| 686 | |||
| 687 | # --------------------------------------------------------------------------- | ||
| 688 | # Public method: known_sexes() | ||
| 689 | # --------------------------------------------------------------------------- | ||
| 690 | |||
| 691 | =head2 known_sexes | ||
| 692 | |||
| 693 | Returns the list of sex codes accepted by C<name()>. | ||
| 694 | |||
| 695 | =head3 PURPOSE | ||
| 696 | |||
| 697 | Documents and exposes the set of valid C<sex> values so that callers can | ||
| 698 | validate their own input without duplicating knowledge. | ||
| 699 | |||
| 700 | =head3 ARGUMENTS | ||
| 701 | |||
| 702 | None. | ||
| 703 | |||
| 704 | =head3 RETURNS | ||
| 705 | |||
| 706 | A list (or array-ref in scalar context) of valid sex code strings: C<('F', 'M')>. | ||
| 707 | |||
| 708 | =head3 SIDE EFFECTS | ||
| 709 | |||
| 710 | None. | ||
| 711 | |||
| 712 | =head3 EXAMPLE | ||
| 713 | |||
| 714 | my @sexes = $namer->known_sexes(); | ||
| 715 | # ( 'F', 'M' ) | ||
| 716 | |||
| 717 | =head3 API SPECIFICATION | ||
| 718 | |||
| 719 | =head4 Input | ||
| 720 | |||
| 721 | {} # no arguments | ||
| 722 | |||
| 723 | =head4 Output | ||
| 724 | |||
| 725 | { | ||
| 726 | type => ARRAYREF, | ||
| 727 | } | ||
| 728 | |||
| 729 | =head3 FORMAL SPECIFICATION | ||
| 730 | |||
| 731 | known_sexes ______________________________________________ | ||
| 732 | [In] (none) | ||
| 733 | [Out] result : seq String | ||
| 734 | |||
| 735 | result == sort { $SEX_FEMALE, $SEX_MALE } | ||
| 736 | |||
| 737 | =cut | ||
| 738 | |||
| 739 | sub known_sexes { | ||
| 740 | # Return the two valid sex codes in sorted order | ||
| 741 | my @sexes = sort($SEX_FEMALE, $SEX_MALE); | ||
| 742 | return wantarray ? @sexes : \@sexes; | ||
| 743 | } | ||
| 744 | |||
| 745 | 1; | ||
| 746 | |||
| 747 | __END__ | ||
| 748 | |||
| 749 | =head1 CONFIGURATION | ||
| 750 | |||
| 751 | The constructor accepts an optional C<language> key which sets the default | ||
| 752 | language for all subsequent calls to C<name()>: | ||
| 753 | |||
| 754 | my $namer = Genealogy::Relationship::Name->new(language => 'fr'); | ||
| 755 | |||
| 756 | This default can be overridden per-call by passing C<language> to C<name()>. | ||
| 757 | The object is also compatible with C<Object::Configure> for runtime | ||
| 758 | reconfiguration. | ||
| 759 | |||
| 760 | =head2 Error handling | ||
| 761 | |||
| 762 | Errors are dispatched through the following priority chain: | ||
| 763 | |||
| 764 | =over 4 | ||
| 765 | |||
| 766 | =item 1. L<Log::Abstraction> object (preferred) | ||
| 767 | |||
| 768 | Construct a C<Log::Abstraction> object with the desired logger coderef and | ||
| 769 | C<ctx> (typically a C<Gedcom::Individual>), then pass it as C<logger> to | ||
| 770 | C<new()>. On error, this module simply calls C<< $logger->error($msg) >> | ||
| 771 | and Log::Abstraction handles ctx forwarding, formatting, and dispatch. | ||
| 772 | |||
| 773 | use Log::Abstraction; | ||
| 774 | |||
| 775 | my $logger = Log::Abstraction->new( | ||
| 776 | logger => sub { | ||
| 777 | my $args = shift; | ||
| 778 | my $msg = $args->{ctx} | ||
| 779 | ? $args->{ctx}->as_string() . ': ' . join('', @{$args->{message}}) | ||
| 780 | : join('', @{$args->{message}}); | ||
| 781 | complain({ message => $msg, person => $args->{ctx} }); | ||
| 782 | }, | ||
| 783 | ctx => $individual, | ||
| 784 | ); | ||
| 785 | |||
| 786 | my $namer = Genealogy::Relationship::Name->new(logger => $logger); | ||
| 787 | |||
| 788 | =back | ||
| 789 | |||
| 790 | If any handler returns without dying (e.g. a warning-only handler in | ||
| 791 | L<Log::Abstraction>), C<name()> returns C<undef>. | ||
| 792 | |||
| 793 | =head1 DIAGNOSTICS | ||
| 794 | |||
| 795 | =over 4 | ||
| 796 | |||
| 797 | =item steps_to_ancestor not given | ||
| 798 | |||
| 799 | C<steps_to_ancestor> was passed as C<undef>. Passing C<undef> explicitly is | ||
| 800 | distinct from omitting the argument; use a defined non-negative integer. | ||
| 801 | |||
| 802 | =item steps_from_ancestor not given | ||
| 803 | |||
| 804 | As above, for C<steps_from_ancestor>. | ||
| 805 | |||
| 806 | =item sex not given | ||
| 807 | |||
| 808 | C<sex> was passed as C<undef>. Supply C<'M'> or C<'F'>. | ||
| 809 | |||
| 810 | =back | ||
| 811 | |||
| 812 | =head1 DEPENDENCIES | ||
| 813 | |||
| 814 | L<Carp>, L<Object::Configure> | ||
| 815 | |||
| 816 | Optionally L<Log::Abstraction> (E<gt>= 0.28) for the C<logger>/C<ctx> error | ||
| 817 | dispatch path. | ||
| 818 | L<Params::Get>, L<Params::Validate::Strict>, L<Readonly> | ||
| 819 | |||
| 820 | =head1 BUGS AND LIMITATIONS | ||
| 821 | |||
| 822 | The lookup tables currently cover steps 0-6 in both directions. Relationships | ||
| 823 | further removed (seventh cousin, etc.) return C<undef>. Pull requests adding | ||
| 824 | deeper tables are welcome. | ||
| 825 | |||
| 826 | =head1 SEE ALSO | ||
| 827 | |||
| 828 | =over 4 | ||
| 829 | |||
| 830 | =item * L<Configure an Object at Runtime|Object::Configure> | ||
| 831 | |||
| 832 | =item * L<Test Dashboard|https://nigelhorne.github.io/Genealogy-Relationship-Name/coverage/> | ||
| 833 | |||
| 834 | =item * L<Gedcom::Individual>, L<Genealogy::Relationship> | ||
| 835 | |||
| 836 | =back | ||
| 837 | |||
| 838 | =head1 AUTHOR | ||
| 839 | |||
| 840 | Nigel Horne C<< <njh@nigelhorne.com> >> | ||
| 841 | |||
| 842 | =head1 REPOSITORY | ||
| 843 | |||
| 844 | L<https://github.com/nigelhorne/Genealogy-Relationship-Name> | ||
| 845 | |||
| 846 | =head1 SUPPORT | ||
| 847 | |||
| 848 | This module is provided as-is without any warranty. | ||
| 849 | |||
| 850 | Please report any bugs or feature requests to C<bug-genealogy-relationship-name at rt.cpan.org>, | ||
| 851 | or through the web interface at | ||
| 852 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Genealogy-Relationship-Name>. | ||
| 853 | I will be notified, and then you'll | ||
| 854 | automatically be notified of progress on your bug as I make changes. | ||
| 855 | |||
| 856 | You can find documentation for this module with the perldoc command. | ||
| 857 | |||
| 858 | perldoc Genealogy::Relationship::Name | ||
| 859 | |||
| 860 | You can also look for information at: | ||
| 861 | |||
| 862 | =over 4 | ||
| 863 | |||
| 864 | =item * MetaCPAN | ||
| 865 | |||
| 866 | L<https://metacpan.org/dist/Genealogy-Relationship-Name> | ||
| 867 | |||
| 868 | =item * RT: CPAN's request tracker | ||
| 869 | |||
| 870 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Genealogy-Relationship-Name> | ||
| 871 | |||
| 872 | =item * CPAN Testers' Matrix | ||
| 873 | |||
| 874 | L<http://matrix.cpantesters.org/?dist=Genealogy-Relationship-Name> | ||
| 875 | |||
| 876 | =item * CPAN Testers Dependencies | ||
| 877 | |||
| 878 | L<http://deps.cpantesters.org/?module=Genealogy::Relationship::Name> | ||
| 879 | |||
| 880 | =back | ||
| 881 | |||
| 882 | =head1 LICENCE AND COPYRIGHT | ||
| 883 | |||
| 884 | Copyright 2026 Nigel Horne. | ||
| 885 | |||
| 886 | Usage is subject to the GPL2 licence terms. | ||
| 887 | If you use it, | ||
| 888 | please let me know. | ||
| 889 | |||
| 890 | =cut |