File Coverage

blib/lib/MooseX/Meta/TypeCoercion/Parameterizable.pm
Criterion Covered Total %
statement 26 28 92.8
branch 4 6 66.6
condition n/a
subroutine 5 5 100.0
pod 1 2 50.0
total 36 41 87.8


line stmt bran cond sub pod time code
1             package ## Hide from PAUSE
2             MooseX::Meta::TypeCoercion::Parameterizable;
3              
4 10     10   85 use Moose;
  10         32  
  10         93  
5 10     10   68274 use MooseX::NonMoose;
  10         29  
  10         85  
6              
7             extends 'Moose::Meta::TypeCoercion';
8              
9             =head1 NAME
10              
11             MooseX::Meta::TypeCoercion::Parameterizable - Coerce Parameterizable type constraints.
12              
13             =head1 DESCRIPTION
14              
15             Coercion Meta Class, intended to make sure coercions work correctly with
16             parameterized types. You probably won't consume or subclass this class directly
17              
18             =head1 METHODS
19              
20             This class defines the following methods.
21              
22             =head2 add_type_coercions
23              
24             method modification to throw exception should we try to add a coercion on a
25             parameterizable type that is already defined by a constraining value. We do
26             this since defined parameterizable type constraints inherit their coercion from
27             the parent constraint. It makes no sense to even be using parameterizable
28             types if you know the constraining value beforehand!
29              
30             =cut
31              
32             around 'add_type_coercions' => sub {
33             my ($add_type_coercions, $self, @args) = @_;
34             if($self->type_constraint->has_constraining_value) {
35             Moose->throw_error("Cannot add type coercions to a parameterizable type constraint that's been defined.");
36             } else {
37             return $self->$add_type_coercions(@args);
38             }
39             };
40              
41             ## These two are here until I can merge change upstream to Moose. These are two
42             ## very minor changes we can probably just put into Moose without breaking stuff.
43             ## Hopefully can can eventually stop doing this.
44              
45             my $self = shift @_;
46 16     16 1 42 my $coderef = $self->_compiled_type_coercion;
47 16         592 return $coderef->(@_); ## <== in Moose we don't call on @_, but $_[1]
48 16         158 }
49              
50             my $self = shift;
51             my @coercion_map = @{$self->type_coercion_map};
52 124     124 0 52762 my @coercions;
53 124         244 while (@coercion_map) {
  124         3874  
54 124         1158 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
55 124         424  
56 9         1695 my $type_constraint = ref $constraint_name
57             ? $constraint_name
58 9 50       34 : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name);
59              
60             unless ( defined $type_constraint ) {
61             require Moose;
62 9 50       26 Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from");
63 0         0 }
64 0         0  
65             push @coercions => [
66             $type_constraint->_compiled_type_constraint,
67 9         71 $action
68             ];
69             }
70             $self->_compiled_type_coercion(sub {
71             my $thing = shift;
72             foreach my $coercion (@coercions) {
73 16     16   34 my ($constraint, $converter) = @$coercion;
74 16         63 if ($constraint->($thing)) {
75 19         100 local $_ = $thing;
76 19 100       345 return $converter->($thing, @_); ## <== Here also we pass @_ which Moose doesn't
77 11         98 }
78 11         40 }
79             return $thing;
80             });
81 5         60 }
82 124         8244  
83             =head1 SEE ALSO
84              
85             The following modules or resources may be of interest.
86              
87             L<Moose>, L<Moose::Meta::TypeCoercion>
88              
89             =head1 AUTHOR
90              
91             John Napiorkowski, C<< <jjnapiork@cpan.org> >>
92              
93             =head1 COPYRIGHT & LICENSE
94              
95             This program is free software; you can redistribute it and/or modify
96             it under the same terms as Perl itself.
97              
98             =cut
99              
100              
101             __PACKAGE__->meta->make_immutable(inline_constructor => 0);
102