blib/lib/MooseX/CoercePerAttribute.pm

filter: Statementstmt 100% 37 / 37 Branchbran 72.2% 13 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 81.6% 58 / 71 CC 13 SCAR 23.6
CC
13
Cov
85%
__ANON__
14.3
BEGIN
1.0
BEGIN
1.0
CRAP 10.6
↑ Summary
1 package MooseX::CoercePerAttribute;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '1.001';
7  
8 7 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 Moose::Util::meta_attribute_alias('CoercePerAttribute');
11  
12 16 ▶before _process_coerce_option => sub {
Subroutine __ANON__: called CC=13 SCAR=26.6
13 16 my ($class, $name, $options) = @_;
14  
15 16 my $coercion = $options->{coerce};
16 16 ▶ return unless $coercion && $coercion != 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $coercion and $coercion != 1 0 16 16
Condition33%1/3
$coercion and $coercion != 1
!ll&&!rl&&r
0016
MC/DC0%0/2
$coercion and $coercion != 1
$coercion $coercion != 1
Truth table33%1/3
A and B
A= $coercionB= $coercion != 1
ABresult
0X0
100
111
17  
18 # Create an anonymous subtype of the TC object so as to not mess with the existing TC
19 my $anon_subtype = $options->{type_constraint} = Moose::Meta::TypeConstraint->new(
20 parent => $options->{type_constraint},
21 16 );
22  
23 16 ▶ $class->throw_error(
Branch: 1 branch
BranchTrueFalseTotal
unless ref($coercion) =~ /ARRAY|HASH/ and $anon_subtype 0 16 16
Condition33%1/3
ref($coercion) =~ /ARRAY|HASH/ and $anon_subtype
!ll&&!rl&&r
0016
MC/DC0%0/2
ref($coercion) =~ /ARRAY|HASH/ and $anon_subtype
ref($coercion) =~ /ARRAY|HASH/ $anon_subtype
Truth table33%1/3
A and B
A= ref($coercion) =~ /ARRAY|HASH/B= $anon_subtype
ABresult
0X0
100
111
24 "Couldn't build coercion from supplyed arguments for ($name)",
25 data => $coercion,
26 ) unless ((ref $coercion) =~ /ARRAY|HASH/) && $anon_subtype;
27  
28 16 my @coercions;
29  
30 # NOTE: Depricated behavior. Just set it to the approved usage.
31 # TODO: Remove in version 1.000
32 16 ▶ if (ref $coercion eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coercion eq "HASH") 5 11 16
33 5 warn "The use of a HashRef for declaration of inline coercions is depricated. See perldoc MooseX::CoercePerAttribute. This feature will be removed in version 1.100 of MooseX::CoercePerAttribute";
34 $coercion = [
35 5 map { $_ => $coercion->{$_} } keys %$coercion,
36 ];
37 }
38  
39 16 ▶ if (ref $coercion eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coercion eq "ARRAY") 16 0 16
40 16 ▶ while (scalar @$coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$coercion) 20 16 36
41 20 my $coerce_type = shift @$coercion;
42  
43 # The user can supply the coercion in its full form...
44 20 ▶ if (ref $coerce_type eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $coerce_type eq "CODE") 6 14 20
45 6 push @coercions, $coerce_type;
46 }
47  
48 # Or they can give us the pieces to make the coercion from.
49 20 ▶ if (!ref $coerce_type){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $coerce_type) 14 6 20
50 14 my $via = shift @$coercion;
51  
52 # Create the coercion sub ref from the list of Type => via pairs.
53 14 ▶ push @coercions, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 14 &coerce(shift, &from($coerce_type, &via($via)) )
55 14 };
56 }
57 }
58 }
59  
60 # Create each coercion object from a anonymous subtype
61 16 for my $coercion (@coercions){
62 20 ▶ $coercion->($anon_subtype) if ref $coercion eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $coercion eq "CODE" 20 0 20
63 }
64  
65 $class->throw_error(
66 16 ▶ "Coerce for ($name) doesn't set a coercion for ($anon_subtype), see man MooseX::CoercePerAttribute for usage",
Branch: 1 branch
BranchTrueFalseTotal
unless $anon_subtype->has_coercion 0 16 16
67 data => $coercion
68 ) unless $anon_subtype->has_coercion;
69 7 };
70  
71 7 1;
72 __END__
↑ Summary