1 package Moose::Meta::TypeConstraint::Parameterized;
7 use Scalar::Util 'blessed';
9 use Moose::Util::TypeConstraints;
11 our $VERSION = '0.02';
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::TypeConstraint';
16 __PACKAGE__->meta->add_attribute('type_parameter' => (
17 accessor => 'type_parameter',
18 predicate => 'has_type_parameter',
21 sub compile_type_constraint {
24 ($self->has_type_parameter)
25 || confess "You cannot create a Higher Order type without a type parameter";
27 my $type_parameter = $self->type_parameter;
29 (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint'))
30 || confess "The type parameter must be a Moose meta type";
33 my $name = $self->parent->name;
36 Moose::Util::TypeConstraints::find_type_constraint('ArrayRef')
40 Moose::Util::TypeConstraints::find_type_constraint('HashRef')
43 my $array_constraint = sub {
45 ($type_parameter->check($x)) || return
49 my $hash_constraint = sub {
50 foreach my $x (values %$_) {
51 ($type_parameter->check($x)) || return
55 if ($self->is_subtype_of('ArrayRef')) {
56 $constraint = $array_constraint;
58 elsif ($self->is_subtype_of('HashRef')) {
59 $constraint = $hash_constraint;
61 elsif ($array_coercion && $array_coercion->has_coercion_for_type($name)) {
63 local $_ = $array_coercion->coerce($_);
64 $array_constraint->(@_);
67 elsif ($hash_coercion && $hash_coercion->has_coercion_for_type($name)) {
69 local $_ = $hash_coercion->coerce($_);
70 $hash_constraint->(@_);
74 confess "The " . $self->name . " constraint cannot be used, because " . $name . " doesn't subtype or coerce ArrayRef or HashRef.";
77 $self->_set_constraint($constraint);
79 $self->SUPER::compile_type_constraint;
91 Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose
99 =item B<compile_type_constraint>
101 =item B<type_parameter>
103 =item B<has_type_parameter>
111 All complex software has bugs lurking in it, and this module is no
112 exception. If you find a bug please either email me, or add the bug
117 Stevan Little E<lt>stevan@iinteractive.comE<gt>
119 =head1 COPYRIGHT AND LICENSE
121 Copyright 2006, 2007 by Infinity Interactive, Inc.
123 L<http://www.iinteractive.com>
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.