| 1 | package Cikl::AddressBuilder; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Module::Pluggable search_path => "Cikl::Models::Address", require => 1, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | sub_name => "_plugins", on_require_error => \&croak; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | require Exporter; | |||||||||
| 10 | 1 | our @ISA = qw/Exporter/; | |||||||||
| 11 | 1 | our @EXPORT_OK = qw/address_from_protoevent create_address create_addresses/; | |||||||||
| 12 | |||||||||||
| 13 | 1 | my $type_map = _build_type_map(); | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | sub _build_type_map { | ||||||||
|
Subroutine
_build_type_map: called CC=4 SCAR=15.0
| |||||||||||
| 16 | 1 | my $ret = {}; | |||||||||
| 17 | 1 | foreach my $module (_plugins()) { | |||||||||
| 18 | 6 | ▶ | unless ($module->does("Cikl::Models::AddressRole")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 0 | die("$module must implement Cikl::Models::AddressRole"); | |||||||||
| 20 | } | ||||||||||
| 21 | 6 | my $type = $module->type(); | |||||||||
| 22 | 6 | ▶ | if (my $existing = $ret->{$type}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | die("Cannot associate '$module' with the type '$type'. Already registered with $existing."); | |||||||||
| 24 | } | ||||||||||
| 25 | 6 | $ret->{$type} = $module; | |||||||||
| 26 | } | ||||||||||
| 27 | 1 | return $ret; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | sub create_address { | ||||||||
|
Subroutine
create_address: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 31 | 0 | my $type = shift; | |||||||||
| 32 | 0 | my $value = shift; | |||||||||
| 33 | 0 | my $type_class = $type_map->{$type}; | |||||||||
| 34 | 0 | ▶ | unless($type_class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | die("Unknown type: $type"); | |||||||||
| 36 | } | ||||||||||
| 37 | 0 | return $type_class->new_normalized(value => $value); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | sub create_addresses { | ||||||||
|
Subroutine
create_addresses: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 41 | 0 | my $protoevent = shift; # hashref | |||||||||
| 42 | 0 | my @ret; | |||||||||
| 43 | 0 | foreach my $type (keys(%{$type_map})) { | |||||||||
| 44 | 0 | ▶ | if (my $value = delete($protoevent->{$type})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | push(@ret, create_address($type, $value)); | |||||||||
| 46 | } | ||||||||||
| 47 | } | ||||||||||
| 48 | 0 | return \@ret; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub address_from_protoevent { | ||||||||
|
Subroutine
address_from_protoevent: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 52 | 0 | my $protoevent = shift; # hashref | |||||||||
| 53 | 0 | my $address; | |||||||||
| 54 | 0 | foreach my $type (keys(%{$type_map})) { | |||||||||
| 55 | 0 | ▶ | if (my $value = delete($protoevent->{$type})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | ▶ | if (defined($address)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | die("An event can only have one address! Has: " . $address->type() . " and $type"); | |||||||||
| 58 | } | ||||||||||
| 59 | 0 | $address = create_address($type, $value); | |||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | 0 | return $address; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 1 | 1; | |||||||||
| 66 | |||||||||||