1 package ## Hide from PAUSE
2 MooseX::Dependent::Meta::TypeCoercion::Dependent;
5 extends 'Moose::Meta::TypeCoercion';
9 MooseX::Meta::TypeCoercion::Dependent - Coerce Dependent type constraints.
13 This class is not intended for public consumption. Please don't subclass it
14 or rely on it. Chances are high stuff here is going to change a lot.
18 This class defines the following methods.
20 =head add_type_coercions
22 method modification to throw exception should we try to add a coercion on a
23 dependent type that is already defined by a constraining value. We do this
24 since defined dependent type constraints inherit their coercion from the parent
25 constraint. It makes no sense to even be using dependent types if you know the
26 constraining value beforehand!
30 around 'add_type_coercions' => sub {
31 my ($add_type_coercions, $self, @args) = @_;
32 if($self->type_constraint->has_constraining_value) {
33 Moose->throw_error("Cannot add type coercions to a dependent type constraint that's been defined.");
35 return $self->$add_type_coercions(@args);
40 ## These two are here until I can merge change upstream to Moose. These are two
41 ## very minor changes we can probably just put into Moose without breaking stuff
44 my $coderef = $self->_compiled_type_coercion;
45 return $coderef->(@_);
48 sub compile_type_coercion {
50 my @coercion_map = @{$self->type_coercion_map};
52 while (@coercion_map) {
53 my ($constraint_name, $action) = splice(@coercion_map, 0, 2);
54 my $type_constraint = ref $constraint_name ? $constraint_name : Moose::Util::TypeConstraints::find_or_parse_type_constraint($constraint_name);
56 unless ( defined $type_constraint ) {
58 Moose->throw_error("Could not find the type constraint ($constraint_name) to coerce from");
62 $type_constraint->_compiled_type_constraint,
66 $self->_compiled_type_coercion(sub {
68 foreach my $coercion (@coercions) {
69 my ($constraint, $converter) = @$coercion;
70 if ($constraint->($thing)) {
72 return $converter->($thing, @_);
81 The following modules or resources may be of interest.
83 L<Moose>, L<Moose::Meta::TypeCoercion>
87 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
89 =head1 COPYRIGHT & LICENSE
91 This program is free software; you can redistribute it and/or modify
92 it under the same terms as Perl itself.
96 __PACKAGE__->meta->make_immutable;