File Coverage

blib/lib/Cikl/AddressBuilder.pm
Criterion Covered Total %
statement 22 46 47.8
branch 2 12 16.6
condition n/a
subroutine 6 9 66.6
pod 0 3 0.0
total 30 70 42.8


line stmt bran cond sub pod time code
1             package Cikl::AddressBuilder;
2 1     1   5 use strict;
  1         18  
  1         28  
3 1     1   4 use warnings;
  1         2  
  1         21  
4 1     1   5 use Carp;
  1         1  
  1         73  
5 1         11 use Module::Pluggable search_path => "Cikl::Models::Address", require => 1,
6 1     1   718 sub_name => "_plugins", on_require_error => \&croak;
  1         21954  
7              
8 1     1   116 use namespace::autoclean;
  1         2  
  1         11  
9             require Exporter;
10             our @ISA = qw/Exporter/;
11             our @EXPORT_OK = qw/address_from_protoevent create_address create_addresses/;
12              
13             my $type_map = _build_type_map();
14              
15             sub _build_type_map {
16 1     1   2 my $ret = {};
17 1         5 foreach my $module (_plugins()) {
18 6 50       5124 unless ($module->does("Cikl::Models::AddressRole")) {
19 0         0 die("$module must implement Cikl::Models::AddressRole");
20             }
21 6         252 my $type = $module->type();
22 6 50       17 if (my $existing = $ret->{$type}) {
23 0         0 die("Cannot associate '$module' with the type '$type'. Already registered with $existing.");
24             }
25 6         20 $ret->{$type} = $module;
26             }
27 1         3 return $ret;
28             }
29              
30             sub create_address {
31 0     0 0   my $type = shift;
32 0           my $value = shift;
33 0           my $type_class = $type_map->{$type};
34 0 0         unless($type_class) {
35 0           die("Unknown type: $type");
36             }
37 0           return $type_class->new_normalized(value => $value);
38             }
39              
40             sub create_addresses {
41 0     0 0   my $protoevent = shift; # hashref
42 0           my @ret;
43 0           foreach my $type (keys(%{$type_map})) {
  0            
44 0 0         if (my $value = delete($protoevent->{$type})) {
45 0           push(@ret, create_address($type, $value));
46             }
47             }
48 0           return \@ret;
49             }
50              
51             sub address_from_protoevent {
52 0     0 0   my $protoevent = shift; # hashref
53 0           my $address;
54 0           foreach my $type (keys(%{$type_map})) {
  0            
55 0 0         if (my $value = delete($protoevent->{$type})) {
56 0 0         if (defined($address)) {
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;
66