Add some support for coercing to ArrayRef or HashRef for collection purposes
[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';
acb8a5db 9use Moose::Util::TypeConstraints;
d67145ed 10
11our $VERSION = '0.01';
12our $AUTHORITY = 'cpan:STEVAN';
13
14use base 'Moose::Meta::TypeConstraint';
15
0fbd4b0a 16__PACKAGE__->meta->add_attribute('type_parameter' => (
17 accessor => 'type_parameter',
18 predicate => 'has_type_parameter',
d67145ed 19));
20
21sub compile_type_constraint {
22 my $self = shift;
23
0fbd4b0a 24 ($self->has_type_parameter)
25 || confess "You cannot create a Higher Order type without a type parameter";
d67145ed 26
0fbd4b0a 27 my $type_parameter = $self->type_parameter;
d67145ed 28
0fbd4b0a 29 (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint'))
30 || confess "The type parameter must be a Moose meta type";
d67145ed 31
32 my $constraint;
acb8a5db 33 my $name = $self->parent->name;
34
35 my $array_coercion =
36 Moose::Util::TypeConstraints::find_type_constraint('ArrayRef')
37 ->coercion;
38
39 my $hash_coercion =
40 Moose::Util::TypeConstraints::find_type_constraint('HashRef')
41 ->coercion;
42
39aba5c9 43 my $array_constraint = sub {
44 foreach my $x (@$_) {
45 ($type_parameter->check($x)) || return
46 } 1;
47 };
48
49 my $hash_constraint = sub {
50 foreach my $x (values %$_) {
51 ($type_parameter->check($x)) || return
52 } 1;
53 };
54
55 if ($self->is_subtype_of('ArrayRef')) {
56 $constraint = $array_constraint;
d67145ed 57 }
39aba5c9 58 elsif ($self->is_subtype_of('HashRef')) {
59 $constraint = $hash_constraint;
d67145ed 60 }
acb8a5db 61 elsif ($array_coercion && $array_coercion->has_coercion_for_type($name)) {
62 $constraint = sub {
63 local $_ = $array_coercion->coerce($_);
64 $array_constraint->(@_);
65 };
66 }
67 elsif ($hash_coercion && $hash_coercion->has_coercion_for_type($name)) {
68 $constraint = sub {
69 local $_ = $hash_coercion->coerce($_);
70 $hash_constraint->(@_);
71 };
72 }
d67145ed 73 else {
acb8a5db 74 confess "The " . $self->name . " constraint cannot be used, because " . $name . " doesn't subtype or coerce ArrayRef or HashRef.";
d67145ed 75 }
76
77 $self->_set_constraint($constraint);
78
79 $self->SUPER::compile_type_constraint;
80}
81
821;
83
84__END__
85
86
87=pod
88
89=head1 NAME
90
0fbd4b0a 91Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose
d67145ed 92
93=head1 DESCRIPTION
94
95=head1 METHODS
96
97=over 4
98
99=item B<compile_type_constraint>
100
0fbd4b0a 101=item B<type_parameter>
d67145ed 102
0fbd4b0a 103=item B<has_type_parameter>
d67145ed 104
105=item B<meta>
106
107=back
108
109=head1 BUGS
110
111All complex software has bugs lurking in it, and this module is no
112exception. If you find a bug please either email me, or add the bug
113to cpan-RT.
114
115=head1 AUTHOR
116
117Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119=head1 COPYRIGHT AND LICENSE
120
121Copyright 2006, 2007 by Infinity Interactive, Inc.
122
123L<http://www.iinteractive.com>
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself.
127
128=cut