blib/lib/MooseX/Meta/TypeConstraint/ForceCoercion.pm

filter: Statementstmt 100% 35 / 35 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.7% 54 / 57 CC 8 SCAR 9.4
CC
8
Cov
94%
AUTOLOAD
3.1
isa
3.0
validate
3.0
CRAP 2.6
↑ Summary
1 package MooseX::Meta::TypeConstraint::ForceCoercion;
2 1 our $VERSION = '0.01';
3  
4 # ABSTRACT: Force coercion when validating type constraints
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 1 has _type_constraint => (
12 is => 'ro',
13 isa => 'Moose::Meta::TypeConstraint',
14 init_arg => 'type_constraint',
15 required => 1,
16 );
17  
18  
19 3 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod documented
20 3 my ($self, $value) = @_;
21 3 my $coerced = $self->_type_constraint->coerce($value);
22 3 ▶ return undef if $coerced == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $coerced == $value 1 2 3
23 2 return $self->_type_constraint->check($coerced);
24 }
25  
26  
27 6 ▶sub validate {
Subroutine validate: called CC=3 SCAR=11.0
Pod documented
28 6 my ($self, $value, $coerced_ref) = @_;
29 6 my $coerced = $self->_type_constraint->coerce($value);
30 6 ▶ return 'Coercion failed' if $coerced == $value;
Branch: 1 branch
BranchTrueFalseTotal
if $coerced == $value 2 4 6
31 4 ▶ ${ $coerced_ref } = $coerced if $coerced_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $coerced_ref 2 2 4
32 4 return $self->_type_constraint->validate($coerced);
33 }
34  
35 1 my $meta = __PACKAGE__->meta;
36  
37 1 for my $meth (qw/isa can meta/) {
38 3 my $orig = __PACKAGE__->can($meth);
39 3 ▶ $meta->add_method($meth => sub {
Subroutine isa: called CC=3 SCAR=11.0
40 3 my ($self) = shift;
41 3 ▶ return $self->$orig(@_) unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 2 1 3
42  
43 1 my $tc = $self->_type_constraint;
44 # this might happen during global destruction
45 1 ▶ return $self->$orig(@_) unless $tc;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc 0 1 1
46  
47 1 return $tc->$meth(@_);
48 3 });
49 }
50  
51 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
52 1 my $self = shift;
53 1 my ($meth) = (our $AUTOLOAD =~ /([^:]+)$/);
54 1 ▶ return unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 1 1
55  
56 1 my $tc = $self->_type_constraint;
57 1 ▶ return unless $tc;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc 0 1 1
58  
59 1 return $tc->$meth(@_);
60 }
61  
62 1 $meta->make_immutable;
63  
64 1 1;
65  
66 __END__
↑ Summary