TraitFor/Meta/TypeConstraint/Mooish.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 90.9% 40 / 44 CC 2 SCAR 2.6
CC
2
Cov
91%
__ANON__
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.3
↑ Summary Meta/TypeConstraint/Mooish.pm »
1 #
2 # This file is part of MooseX-Meta-TypeConstraint-Mooish
3 #
4 # This software is Copyright (c) 2015 by Chris Weyl.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package MooseX::TraitFor::Meta::TypeConstraint::Mooish;
11 4 our $AUTHORITY = 'cpan:RSRCHBOY';
12 4 $MooseX::TraitFor::Meta::TypeConstraint::Mooish::VERSION = '0.001';
13 # ABSTRACT: Handle Moo-style constraints
14  
15 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use namespace::autoclean 0.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 4 has original_constraint => (
21 is => 'ro',
22 isa => 'CodeRef',
23 writer => '_set_original_constraint',
24 predicate => 'has_original_constraint',
25 );
26  
27  
28 4 has mooish => (
29 is => 'ro',
30 isa => 'Bool',
31 default => 1,
32 );
33  
34  
35 1 ▶before compile_type_constraint => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
36 1 my $self = shift @_;
37  
38 ### only wrap our given constraint iff we're supposed to be mooish...
39 1 ▶ return unless $self->mooish;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mooish 0 1 1
40  
41 ### wrap our type constraint, and set it...
42 1 my $wrapped_constraint = $self->_wrap_constraint($self->constraint);
43 1 $self->_set_original_constraint($self->constraint);
44 1 $self->_set_constraint($wrapped_constraint);
45  
46 1 return;
47 4 };
48  
49 1 ▶sub _wrap_constraint {
Subroutine _wrap_constraint: called CC=1 SCAR=0.0
50 1 my ($self, $constraint) = @_;
51  
52 # call the original constraint. if it does not die, return true; if it
53 # does die, return false. We might do something with the fail message
54 # down the road, but not right now.
55  
56 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 2 my @args = @_;
58 2 ▶ my $fail_msg = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 local $_ = $args[0];
60 2 $constraint->(@args);
61 1 return;
62 }
63 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 1 return $_;
65 2 };
66  
67 2 return !$fail_msg;
68 1 };
69 }
70  
71  
72 0 ▶around create_child_type => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my ($orig, $self) = (shift, shift);
74  
75 0 return $self->$orig(mooish => 0, @_);
76 4 };
77  
78 4 !!42;
79  
80 __END__
↑ Summary Meta/TypeConstraint/Mooish.pm »