changelog
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
CommitLineData
7e4e1ad4 1package Moose::Meta::TypeConstraint::Parameterizable;
2
3use strict;
4use warnings;
5use metaclass;
6
a94188ac 7our $VERSION = '0.56';
7e4e1ad4 8our $AUTHORITY = 'cpan:STEVAN';
9
10use base 'Moose::Meta::TypeConstraint';
11
12__PACKAGE__->meta->add_attribute('constraint_generator' => (
13 accessor => 'constraint_generator',
14 predicate => 'has_constraint_generator',
15));
16
17sub generate_constraint_for {
18 my ($self, $type) = @_;
19
20 return unless $self->has_constraint_generator;
21
22 return $self->constraint_generator->($type->type_parameter)
23 if $type->is_subtype_of($self->name);
24
25 return $self->_can_coerce_constraint_from($type)
26 if $self->has_coercion
27 && $self->coercion->has_coercion_for_type($type->parent->name);
28
29 return;
30}
31
32sub _can_coerce_constraint_from {
33 my ($self, $type) = @_;
34 my $coercion = $self->coercion;
35 my $constraint = $self->constraint_generator->($type->type_parameter);
36 return sub {
37 local $_ = $coercion->coerce($_);
38 $constraint->(@_);
39 };
40}
41
42
431;
44
45__END__
46
47
48=pod
49
50=head1 NAME
51
52Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for Moose
53
54=head1 METHODS
55
56=over 4
57
58=item B<constraint_generator>
59
60=item B<has_constraint_generator>
61
62=item B<generate_constraint_for>
63
64=item B<meta>
65
66=back
67
68=head1 BUGS
69
70All complex software has bugs lurking in it, and this module is no
71exception. If you find a bug please either email me, or add the bug
72to cpan-RT.
73
74=head1 AUTHOR
75
76Stevan Little E<lt>stevan@iinteractive.comE<gt>
77
78=head1 COPYRIGHT AND LICENSE
79
80Copyright 2006-2008 by Infinity Interactive, Inc.
81
82L<http://www.iinteractive.com>
83
84This library is free software; you can redistribute it and/or modify
85it under the same terms as Perl itself.
86
87=cut