Merge 'trunk' into 'parameterize_constructor_role'
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
CommitLineData
7e4e1ad4 1package Moose::Meta::TypeConstraint::Parameterizable;
2
3use strict;
4use warnings;
5use metaclass;
6
fb4fcfee 7our $VERSION = '0.57';
75b95414 8$VERSION = eval $VERSION;
7e4e1ad4 9our $AUTHORITY = 'cpan:STEVAN';
10
11use base 'Moose::Meta::TypeConstraint';
79135d09 12use Moose::Meta::TypeConstraint::Parameterized;
bb6c8335 13use Moose::Util::TypeConstraints ();
7e4e1ad4 14
15__PACKAGE__->meta->add_attribute('constraint_generator' => (
16 accessor => 'constraint_generator',
17 predicate => 'has_constraint_generator',
18));
19
20sub generate_constraint_for {
21 my ($self, $type) = @_;
22
23 return unless $self->has_constraint_generator;
24
25 return $self->constraint_generator->($type->type_parameter)
26 if $type->is_subtype_of($self->name);
27
28 return $self->_can_coerce_constraint_from($type)
29 if $self->has_coercion
30 && $self->coercion->has_coercion_for_type($type->parent->name);
31
32 return;
33}
34
35sub _can_coerce_constraint_from {
36 my ($self, $type) = @_;
37 my $coercion = $self->coercion;
38 my $constraint = $self->constraint_generator->($type->type_parameter);
39 return sub {
40 local $_ = $coercion->coerce($_);
41 $constraint->(@_);
42 };
43}
44
af694177 45sub parse_parameter_str {
46 my ($self, $type_str) = @_;
bb6c8335 47 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_str);
af694177 48}
49
79135d09 50sub parameterize {
08380fdb 51 my ($self, $contained_tc) = @_;
52
53 if($contained_tc->isa('Moose::Meta::TypeConstraint')) {
54 my $tc_name = $self->name .'['. $contained_tc->name .']';
55 return Moose::Meta::TypeConstraint::Parameterized->new(
56 name => $tc_name,
57 parent => $self,
58 type_parameter => $contained_tc,
59 );
60 } else {
61 Moose->throw_error("The type parameter must be a Moose meta type");
2dd0aea3 62 }
79135d09 63}
64
7e4e1ad4 65
661;
67
68__END__
69
70
71=pod
72
73=head1 NAME
74
75Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for Moose
76
77=head1 METHODS
78
79=over 4
80
81=item B<constraint_generator>
82
83=item B<has_constraint_generator>
84
85=item B<generate_constraint_for>
86
af694177 87=item B<parse_parameter_str>
88
89Given a string, convert it to a Perl structure.
90
2dd0aea3 91=item B<parameterize>
92
93Given an array of type constraints, parameterize the current type constraint.
94
7e4e1ad4 95=item B<meta>
96
97=back
98
99=head1 BUGS
100
101All complex software has bugs lurking in it, and this module is no
102exception. If you find a bug please either email me, or add the bug
103to cpan-RT.
104
105=head1 AUTHOR
106
107Stevan Little E<lt>stevan@iinteractive.comE<gt>
108
109=head1 COPYRIGHT AND LICENSE
110
111Copyright 2006-2008 by Infinity Interactive, Inc.
112
113L<http://www.iinteractive.com>
114
115This library is free software; you can redistribute it and/or modify
116it under the same terms as Perl itself.
117
118=cut