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