Courriel/Role/HeaderWithAttributes.pm

filter: Statementstmt 93.4% 86 / 92 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 2 totaltotal 87.0% 121 / 139 CC 12 SCAR 7.9
CC
12
Cov
89%
__ANON__
5.0
__ANON__
2.5
as_header_value
2.5
CRAP 2.2
« Email/Abstract/Courriel.pm ↑ Summary Courriel/Header/ContentType.pm »
1 package Courriel::Role::HeaderWithAttributes;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '0.49';
8  
9 9 ▶use Courriel::HeaderAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Courriel::Helpers qw( parse_header_with_attributes );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Courriel::Types qw( HashRef NonEmptyStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Params::ValidationCompiler qw( validation_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 parameter main_value_key => (
18 isa => NonEmptyStr,
19 required => 1,
20 );
21  
22 6 parameter main_value_method => (
23 isa => NonEmptyStr,
24 );
25  
26 has _attributes => (
27 traits => ['Hash'],
28 is => 'ro',
29 isa => HashRef ['Courriel::HeaderAttribute'],
30 init_arg => 'attributes',
31 8 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 6 handles => {
33 attributes => 'elements',
34 _attribute => 'get',
35 _set_attribute => 'set',
36 _has_attributes => 'count',
37 },
38 );
39  
40 222 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
41 222 my $orig = shift;
42 222 my $class = shift;
43  
44 222 my $p = $class->$orig(@_);
45  
46 return $p
47 222 ▶ unless $p->{attributes} && reftype( $p->{attributes} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $p->{"attributes"} and builtin::reftype($p->{"attributes"}) eq "HASH" 8 214 222
Condition66%2/3
$p->{"attributes"} and builtin::reftype($p->{"attributes"}) eq "HASH"
!ll&&!rl&&r
80214
MC/DC50%1/2
$p->{"attributes"} and builtin::reftype($p->{"attributes"}) eq "HASH"
$p->{"attributes"} builtin::reftype($p->{"attributes"}) eq "HASH"
Truth table66%2/3
A and B
A= $p->{"attributes"}B= builtin::reftype($p->{"attributes"}) eq "HASH"
ABresult
0X0
100
111
48  
49 214 for my $name ( keys %{ $p->{attributes} } ) {
50 156 my $lc_name = lc $name;
51 156 $p->{attributes}{$lc_name} = delete $p->{attributes}{$name};
52  
53 156 ▶ next if blessed( $p->{attributes}{$lc_name} );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($p->{"attributes"}{$lc_name}) 151 5 156
54  
55 $p->{attributes}{$lc_name} = Courriel::HeaderAttribute->new(
56 name => $name,
57 5 value => $p->{attributes}{$name},
58 );
59 }
60  
61 214 return $p;
62 6 };
63  
64 80 ▶sub attribute {
Subroutine attribute: called CC=2 SCAR=7.0
Pod undocumented
65 80 my $self = shift;
66 80 my $key = shift;
67  
68 80 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 80 80
69  
70 80 return $self->_attribute( lc $key );
71 }
72  
73 {
74 6 my $validator = validation_for(
75 params => [ { type => NonEmptyStr } ],
76 );
77  
78 40 ▶ sub attribute_value {
Subroutine attribute_value: called CC=2 SCAR=6.9
Pod undocumented
79 40 my $self = shift;
80 40 my ($name) = $validator->(@_);
81  
82 40 my $attr = $self->attribute($name);
83  
84 40 ▶ return $attr ? $attr->value : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attr ? : 39 1 40
85 }
86 }
87  
88 6 ▶sub _attributes_as_string {
Subroutine _attributes_as_string: called CC=1 SCAR=0.0
89 1 my $self = shift;
90  
91 1 my $attr = $self->_attributes;
92  
93 1 return join '; ', map { $attr->{$_}->as_string } sort keys %{$attr};
94 }
95  
96 {
97 6 my $validator = validation_for(
98 params => [
99 name => { type => NonEmptyStr, optional => 1 },
100 value => { type => NonEmptyStr },
101 ],
102 named_to_list => 1,
103 );
104  
105 17 ▶ role {
Subroutine __ANON__: called CC=2 SCAR=7.2
106 17 my $p = shift;
107  
108 17 my $main_value_key = $p->main_value_key;
109  
110 194 ▶ method new_from_value => sub {
Subroutine new_from_value: called CC=2 SCAR=6.9
Subroutine __ANON__: called CC=2 SCAR=6.9
111 194 my $class = shift;
112 194 my ( $name, $value ) = $validator->(@_);
113  
114 194 my ( $main_value, $attributes )
115 = parse_header_with_attributes($value);
116  
117 194 my %p = (
118 value => $value,
119 $main_value_key => $main_value,
120 attributes => $attributes,
121 );
122  
123 194 ▶ $p{name} = $name if defined $name;
Branch: 2 branches
BranchTrueFalseTotal
if defined $name 193 1 194
if defined $name 15 0 15
124  
125 194 return $class->new(%p);
126 17 };
127  
128 17 ▶ my $main_value_meth = $p->main_value_method || $p->main_value_key;
Condition66%2/3
$p->main_value_method || $p->main_value_key
l!l&&r!l&&!r
980
MC/DC0%0/2
$p->main_value_method || $p->main_value_key
$p->main_value_method $p->main_value_key
Truth table66%2/3
A || B
A= $p->main_value_methodB= $p->main_value_key
ABresult
1X1
011
000
129  
130 2 ▶ method as_header_value => sub {
Subroutine as_header_value: called CC=2 SCAR=9.2
Subroutine __ANON__: not called CC=2 SCAR=9.2
131 0 my $self = shift;
132  
133 0 my $string = $self->$main_value_meth;
134  
135 0 ▶ if ( $self->_has_attributes ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_has_attributes) 1 1 2
if ($self->_has_attributes) 0 0 0
136 0 $string .= '; ';
137 0 $string .= $self->_attributes_as_string;
138 }
139  
140 0 return $string;
141 17 };
142 }
143 6 }
144  
145 6 1;
« Email/Abstract/Courriel.pm ↑ Summary Courriel/Header/ContentType.pm »