s/container/parameterized/
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterized.pm
CommitLineData
0fbd4b0a 1package Moose::Meta::TypeConstraint::Parameterized;
d67145ed 2
3use strict;
4use warnings;
5use metaclass;
6
7use Scalar::Util 'blessed';
8use Carp 'confess';
9
10our $VERSION = '0.01';
11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Moose::Meta::TypeConstraint';
14
0fbd4b0a 15__PACKAGE__->meta->add_attribute('type_parameter' => (
16 accessor => 'type_parameter',
17 predicate => 'has_type_parameter',
d67145ed 18));
19
20sub compile_type_constraint {
21 my $self = shift;
22
0fbd4b0a 23 ($self->has_type_parameter)
24 || confess "You cannot create a Higher Order type without a type parameter";
d67145ed 25
0fbd4b0a 26 my $type_parameter = $self->type_parameter;
d67145ed 27
0fbd4b0a 28 (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint'))
29 || confess "The type parameter must be a Moose meta type";
d67145ed 30
31 my $constraint;
32
b17a3035 33 my $parent_name = $self->parent->name;
34
d67145ed 35 if ($parent_name eq 'ArrayRef') {
36 $constraint = sub {
37 foreach my $x (@$_) {
0fbd4b0a 38 ($type_parameter->check($x)) || return
d67145ed 39 } 1;
40 };
41 }
42 elsif ($parent_name eq 'HashRef') {
43 $constraint = sub {
44 foreach my $x (values %$_) {
0fbd4b0a 45 ($type_parameter->check($x)) || return
d67145ed 46 } 1;
47 };
48 }
49 else {
50 confess "Your isa must be either ArrayRef or HashRef (sorry no subtype support yet)";
51 }
52
53 $self->_set_constraint($constraint);
54
55 $self->SUPER::compile_type_constraint;
56}
57
581;
59
60__END__
61
62
63=pod
64
65=head1 NAME
66
0fbd4b0a 67Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose
d67145ed 68
69=head1 DESCRIPTION
70
71=head1 METHODS
72
73=over 4
74
75=item B<compile_type_constraint>
76
0fbd4b0a 77=item B<type_parameter>
d67145ed 78
0fbd4b0a 79=item B<has_type_parameter>
d67145ed 80
81=item B<meta>
82
83=back
84
85=head1 BUGS
86
87All complex software has bugs lurking in it, and this module is no
88exception. If you find a bug please either email me, or add the bug
89to cpan-RT.
90
91=head1 AUTHOR
92
93Stevan Little E<lt>stevan@iinteractive.comE<gt>
94
95=head1 COPYRIGHT AND LICENSE
96
97Copyright 2006, 2007 by Infinity Interactive, Inc.
98
99L<http://www.iinteractive.com>
100
101This library is free software; you can redistribute it and/or modify
102it under the same terms as Perl itself.
103
104=cut