1 package Moose::Meta::TypeConstraint::Parameterizable;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 use base 'Moose::Meta::TypeConstraint';
12 use Moose::Meta::TypeConstraint::Parameterized;
13 use Moose::Util::TypeConstraints ();
15 __PACKAGE__->meta->add_attribute('constraint_generator' => (
16 accessor => 'constraint_generator',
17 predicate => 'has_constraint_generator',
20 sub generate_constraint_for {
21 my ($self, $type) = @_;
23 return unless $self->has_constraint_generator;
25 return $self->constraint_generator->($type->type_parameter)
26 if $type->is_subtype_of($self->name);
28 return $self->_can_coerce_constraint_from($type)
29 if $self->has_coercion
30 && $self->coercion->has_coercion_for_type($type->parent->name);
35 sub _can_coerce_constraint_from {
36 my ($self, $type) = @_;
37 my $coercion = $self->coercion;
38 my $constraint = $self->constraint_generator->($type->type_parameter);
40 local $_ = $coercion->coerce($_);
45 sub _parse_type_parameter {
46 my ($self, $type_parameter) = @_;
47 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
51 my ($self, $type_parameter) = @_;
53 my $contained_tc = $self->_parse_type_parameter($type_parameter);
55 if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
56 my $tc_name = $self->name . '[' . $contained_tc->name . ']';
57 return Moose::Meta::TypeConstraint::Parameterized->new(
60 type_parameter => $contained_tc,
64 Moose->throw_error("The type parameter must be a Moose meta type");
78 Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for Moose
84 =item B<constraint_generator>
86 =item B<has_constraint_generator>
88 =item B<generate_constraint_for>
92 Given an array of type constraints, parameterize the current type constraint.
100 All complex software has bugs lurking in it, and this module is no
101 exception. If you find a bug please either email me, or add the bug
106 Stevan Little E<lt>stevan@iinteractive.comE<gt>
108 =head1 COPYRIGHT AND LICENSE
110 Copyright 2006-2008 by Infinity Interactive, Inc.
112 L<http://www.iinteractive.com>
114 This library is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.