Types/TypeDecorator.pm

filter: Statementstmt 91.2% 83 / 91 Branchbran 75.0% 30 / 40 Conditioncond 66.6% 12 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 95.2% 20 / 21 Podpod 100% 3 / 3 totaltotal 82.1% 152 / 185 CC 27 SCAR 17.1
CC
27
Cov
84%
new
11.3
_try_delegate
10.0
__ANON__
5.1
CRAP 5.5
« Types.pm ↑ Summary Types/Combine.pm »
1 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package MooseX::Types::TypeDecorator;
4 # ABSTRACT: Wraps Moose::Meta::TypeConstraint objects with added features
5  
6 18 our $VERSION = '0.51';
7  
8 18 ▶use Carp::Clan '^MooseX::Types';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Moose::Util::TypeConstraints ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Moose::Meta::TypeConstraint::Union;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use namespace::autoclean 0.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 ▶use overload(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶ '0+' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 4 my $self = shift @_;
17 4 my $tc = $self->{__type_constraint};
18 4 return 0+$tc;
19 },
20 # workaround for perl 5.8.5 bug
21 4 ▶ '==' => sub { 0+$_[0] == 0+$_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 187 ▶ '""' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
23 187 my $self = shift @_;
24 187 ▶ if(blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 187 0 187
25 187 return $self->__type_constraint->name;
26 } else {
27 0 return "$self";
28 }
29 },
30 132 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 17 ▶ '|' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
32  
33 ## It's kind of ugly that we need to know about Union Types, but this
34 ## is needed for syntax compatibility. Maybe someday we'll all just do
35 ## Or[Str,Str,Int]
36  
37 17 my @args = @_[0,1]; ## arg 3 is special, see the overload docs.
38 32 my @tc = grep {blessed $_} map {
39 17 ▶ blessed $_ ? $_ :
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 30 4 34
Condition66%2/3
Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) or __PACKAGE__->_throw_error("$_ is not a type constraint")
l!l&&r!l&&!r
310
MC/DC0%0/2
Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) or __PACKAGE__->_throw_error("$_ is not a type constraint")
Moose::Util::TypeConstraints::find_or_parse_type_constraint($_) __PACKAGE__->_throw_error("$_ is not a type constraint")
Truth table33%1/3
A or B
A= Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)B= __PACKAGE__->_throw_error("$_ is not a type constraint")
ABresult
1X1
011
000
40 Moose::Util::TypeConstraints::find_or_parse_type_constraint($_)
41 || __PACKAGE__->_throw_error( "$_ is not a type constraint")
42 } @args;
43  
44 16 ▶ ( scalar @tc == scalar @args)
Branch: 1 branch
BranchTrueFalseTotal
scalar @tc == scalar @args or __PACKAGE__->_throw_error('one of your type constraints is bad. Passed: ' . join(", ", @args) . ' Got: ' . join(", ", @tc)) 16 0 16
45 || __PACKAGE__->_throw_error(
46 "one of your type constraints is bad. Passed: ". join(', ', @args) ." Got: ". join(', ', @tc));
47  
48 16 ▶ ( scalar @tc >= 2 )
Branch: 1 branch
BranchTrueFalseTotal
scalar @tc >= 2 or __PACKAGE__->_throw_error("You must pass in at least 2 type names to make a union") 16 0 16
49 || __PACKAGE__->_throw_error("You must pass in at least 2 type names to make a union");
50  
51 16 my $union = Moose::Meta::TypeConstraint::Union->new(type_constraints=>\@tc);
52 16 return Moose::Util::TypeConstraints::register_type_constraint($union);
53 },
54 18 fallback => 1,
55 18 );
56  
57 #pod =head1 DESCRIPTION
58 #pod
59 #pod This is a decorator object that contains an underlying type constraint. We use
60 #pod this to control access to the type constraint and to add some features.
61 #pod
62 #pod =head1 METHODS
63 #pod
64 #pod This class defines the following methods.
65 #pod
66 #pod =head2 new
67 #pod
68 #pod Old school instantiation
69 #pod
70 #pod =cut
71  
72 252 ▶sub new {
Subroutine new: called CC=8 SCAR=24.2
Pod documented
73 252 my $proto = shift;
74 252 ▶ if (ref($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 3 249 252
75 3 return $proto->_try_delegate('new', @_);
76 }
77 249 my $class = $proto;
78 249 ▶ if(my $arg = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @_) { } 249 0 249
79 249 ▶ if(blessed $arg && $arg->isa('Moose::Meta::TypeConstraint')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($arg) and $arg->isa('Moose::Meta::TypeConstraint')) { } 197 52 249
elsif (builtin::blessed($arg) and $arg->isa('MooseX::Types::UndefinedType')) { } 52 0 52
elsif (builtin::blessed($arg)) { } 0 0 0
Condition66%2/3
builtin::blessed($arg) and $arg->isa("Moose::Meta::TypeConstraint")
!ll&&!rl&&r
052197
MC/DC50%1/2
builtin::blessed($arg) and $arg->isa("Moose::Meta::TypeConstraint")
builtin::blessed($arg) $arg->isa("Moose::Meta::TypeConstraint")
Truth table66%2/3
A and B
A= builtin::blessed($arg)B= $arg->isa("Moose::Meta::TypeConstraint")
ABresult
0X0
100
111
80 197 return bless {'__type_constraint'=>$arg}, $class;
81 ▶ } elsif(
Condition33%1/3
builtin::blessed($arg) and $arg->isa("MooseX::Types::UndefinedType")
!ll&&!rl&&r
0052
MC/DC0%0/2
builtin::blessed($arg) and $arg->isa("MooseX::Types::UndefinedType")
builtin::blessed($arg) $arg->isa("MooseX::Types::UndefinedType")
Truth table33%1/3
A and B
A= builtin::blessed($arg)B= $arg->isa("MooseX::Types::UndefinedType")
ABresult
0X0
100
111
82 blessed $arg &&
83 $arg->isa('MooseX::Types::UndefinedType')
84 ) {
85 ## stub in case we'll need to handle these types differently
86 52 return bless {'__type_constraint'=>$arg}, $class;
87 } elsif(blessed $arg) {
88 0 __PACKAGE__->_throw_error("Argument must be ->isa('Moose::Meta::TypeConstraint') or ->isa('MooseX::Types::UndefinedType'), not ". blessed $arg);
89 } else {
90 0 __PACKAGE__->_throw_error("Argument cannot be '$arg'");
91 }
92 } else {
93 0 __PACKAGE__->_throw_error("This method [new] requires a single argument.");
94 }
95 }
96  
97 #pod =head2 __type_constraint ($type_constraint)
98 #pod
99 #pod Set/Get the type_constraint.
100 #pod
101 #pod =cut
102  
103 933 ▶sub __type_constraint {
Subroutine __type_constraint: called CC=3 SCAR=12.7
104 933 my $self = shift @_;
105 933 ▶ if(blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 933 0 933
106 933 ▶ if(defined(my $tc = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tc = shift @_)) 0 933 933
107 0 $self->{__type_constraint} = $tc;
108 }
109 933 return $self->{__type_constraint};
110 } else {
111 0 __PACKAGE__->_throw_error('cannot call __type_constraint as a class method');
112 }
113 }
114  
115 #pod =head2 C<isa>
116 #pod
117 #pod handle C<< $self->isa >> since C<AUTOLOAD> can't - this tries both the type constraint,
118 #pod and for a class type, the class.
119 #pod
120 #pod =cut
121  
122 154 ▶sub isa {
Subroutine isa: called CC=3 SCAR=11.1
Pod documented
123 154 my $self = shift;
124 return
125 154 ▶ blessed $self
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 63 91 154
Condition66%2/3
$self->__type_constraint->isa(@_) or $self->_try_delegate("isa", @_)
l!l&&r!l&&!r
6003
MC/DC50%1/2
$self->__type_constraint->isa(@_) or $self->_try_delegate("isa", @_)
$self->__type_constraint->isa(@_) $self->_try_delegate("isa", @_)
Truth table66%2/3
A or B
A= $self->__type_constraint->isa(@_)B= $self->_try_delegate("isa", @_)
ABresult
1X1
011
000
126 ? $self->__type_constraint->isa(@_)
127 || $self->_try_delegate( 'isa', @_ )
128 : $self->SUPER::isa(@_);
129 }
130  
131 #pod =head2 can
132 #pod
133 #pod handle $self->can since AUTOLOAD can't.
134 #pod
135 #pod =cut
136  
137 52 ▶sub can {
Subroutine can: called CC=2 SCAR=6.9
Pod documented
138 52 my $self = shift;
139  
140 52 ▶ return blessed $self
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 15 37 52
141 ? $self->_try_delegate( 'can', @_ )
142 : $self->SUPER::can(@_);
143 }
144  
145 #pod =head2 _throw_error
146 #pod
147 #pod properly delegate error messages
148 #pod
149 #pod =cut
150  
151 1 ▶sub _throw_error {
Subroutine _throw_error: called CC=1 SCAR=0.0
152 1 shift;
153 1 require Moose;
154 1 unshift @_, 'Moose';
155 1 goto &Moose::throw_error;
156 }
157  
158 #pod =head2 DESTROY
159 #pod
160 #pod We might need it later
161 #pod
162 #pod =cut
163  
164 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
165 0 return;
166 }
167  
168 #pod =head2 AUTOLOAD
169 #pod
170 #pod Delegate to the decorator target, unless this is a class type, in which
171 #pod case it will try to delegate to the type object, then if that fails try
172 #pod the class. The method 'new' is special cased to only be permitted on
173 #pod the class; if there is no class, or it does not provide a new method,
174 #pod an exception will be thrown.
175 #pod
176 #pod =cut
177  
178 662 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
179 662 my ($self, @args) = @_;
180 662 my ($method) = (our $AUTOLOAD =~ /([^:]+)$/);
181  
182 ## We delegate with this method in an attempt to support a value of
183 ## __type_constraint which is also AUTOLOADing, in particular the class
184 ## MooseX::Types::UndefinedType which AUTOLOADs during autovivication.
185  
186 662 $self->_try_delegate($method, @args);
187 }
188  
189 683 ▶sub _try_delegate {
Subroutine _try_delegate: called CC=10 SCAR=23.1
190 683 my ($self, $method, @args) = @_;
191 683 my $tc = $self->__type_constraint;
192 683 my $class;
193 683 ▶ if ($tc->can('is_subtype_of')) { # Union can't
Branch: 1 branch
BranchTrueFalseTotal
if ($tc->can("is_subtype_of")) 673 10 683
194 673 my $search_tc = $tc;
195 673 while (1) {
196 689 ▶ if ($search_tc->isa('Moose::Meta::TypeConstraint::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_tc->isa("Moose::Meta::TypeConstraint::Class")) 16 673 689
197 16 $class = $search_tc->class;
198 16 last;
199 }
200 673 $search_tc = $search_tc->parent;
201 673 ▶ last unless $search_tc && $search_tc->is_subtype_of('Object');
Branch: 1 branch
BranchTrueFalseTotal
unless $search_tc and $search_tc->is_subtype_of("Object") 657 16 673
Condition100%3/3
$search_tc and $search_tc->is_subtype_of("Object")
!ll&&!rl&&r
165616
MC/DC100%2/2
$search_tc and $search_tc->is_subtype_of("Object")
$search_tc $search_tc->is_subtype_of("Object")
Truth table100%3/3
A and B
A= $search_tcB= $search_tc->is_subtype_of("Object")
ABresult
0X0
100
111
202 }
203 }
204  
205 683 my $inv = do {
206 683 ▶ if ($method eq 'new') {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'new') { } 3 680 683
elsif ($class and not $tc->can($method)) { } 0 680 680
207 3 ▶ die "new called on type decorator for non-class-type ".$tc->name
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 2 3
208 unless $class;
209 2 ▶ die "new called on class type decorator ".$tc->name."\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 1 1 2
210 ." for class ${class}\n"
211 ." which does not provide a new method - did you forget to load it?"
212 unless $class->can('new');
213 1 $class
214 ▶ } elsif ($class && !$tc->can($method)) {
Condition66%2/3
$class and not $tc->can($method)
!ll&&!rl&&r
666140
MC/DC0%0/2
$class and not $tc->can($method)
$class not $tc->can($method)
Truth table66%2/3
A and not(B)
A= $classB= not $tc->can($method)
ABresult
0X0
100
111
215 0 $class
216 } else {
217 680 $tc
218 }
219 };
220  
221 681 $inv->$method(@args);
222 }
223  
224 18 1;
225  
226 __END__
« Types.pm ↑ Summary Types/Combine.pm »