Commit | Line | Data |
0fbd4b0a |
1 | package Moose::Meta::TypeConstraint::Parameterized; |
d67145ed |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
7 | use Scalar::Util 'blessed'; |
acb8a5db |
8 | use Moose::Util::TypeConstraints; |
83526133 |
9 | use Moose::Meta::TypeConstraint::Parameterizable; |
d67145ed |
10 | |
b2ad68e3 |
11 | our $VERSION = '0.67'; |
e606ae5f |
12 | $VERSION = eval $VERSION; |
d67145ed |
13 | our $AUTHORITY = 'cpan:STEVAN'; |
14 | |
15 | use base 'Moose::Meta::TypeConstraint'; |
16 | |
0fbd4b0a |
17 | __PACKAGE__->meta->add_attribute('type_parameter' => ( |
18 | accessor => 'type_parameter', |
19 | predicate => 'has_type_parameter', |
d67145ed |
20 | )); |
21 | |
dabed765 |
22 | sub equals { |
23 | my ( $self, $type_or_name ) = @_; |
24 | |
25 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); |
26 | |
27 | return unless $other->isa(__PACKAGE__); |
28 | |
29 | return ( |
30 | $self->type_parameter->equals( $other->type_parameter ) |
31 | and |
32 | $self->parent->equals( $other->parent ) |
33 | ); |
34 | } |
35 | |
d67145ed |
36 | sub compile_type_constraint { |
37 | my $self = shift; |
38 | |
0fbd4b0a |
39 | ($self->has_type_parameter) |
c245d69b |
40 | || Moose->throw_error("You cannot create a Higher Order type without a type parameter"); |
d67145ed |
41 | |
0fbd4b0a |
42 | my $type_parameter = $self->type_parameter; |
d67145ed |
43 | |
0fbd4b0a |
44 | (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint')) |
c245d69b |
45 | || Moose->throw_error("The type parameter must be a Moose meta type"); |
7e4e1ad4 |
46 | |
47 | foreach my $type (Moose::Util::TypeConstraints::get_all_parameterizable_types()) { |
48 | if (my $constraint = $type->generate_constraint_for($self)) { |
49 | $self->_set_constraint($constraint); |
50 | return $self->SUPER::compile_type_constraint; |
51 | } |
d67145ed |
52 | } |
53 | |
7e4e1ad4 |
54 | # if we get here, then we couldn't |
55 | # find a way to parameterize this type |
c245d69b |
56 | Moose->throw_error("The " . $self->name . " constraint cannot be used, because " |
4c0b3599 |
57 | . $self->parent->name . " doesn't subtype or coerce from a parameterizable type."); |
d67145ed |
58 | } |
59 | |
85a9908f |
60 | sub create_child_type { |
9ceb576e |
61 | my ($self, %opts) = @_; |
83526133 |
62 | return Moose::Meta::TypeConstraint::Parameterizable->new(%opts, parent=>$self); |
9ceb576e |
63 | } |
64 | |
d67145ed |
65 | 1; |
66 | |
67 | __END__ |
68 | |
69 | |
70 | =pod |
71 | |
72 | =head1 NAME |
73 | |
0fbd4b0a |
74 | Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose |
d67145ed |
75 | |
d67145ed |
76 | =head1 METHODS |
77 | |
78 | =over 4 |
79 | |
80 | =item B<compile_type_constraint> |
81 | |
0fbd4b0a |
82 | =item B<type_parameter> |
d67145ed |
83 | |
0fbd4b0a |
84 | =item B<has_type_parameter> |
d67145ed |
85 | |
86 | =item B<meta> |
87 | |
dabed765 |
88 | =item B<equals> |
89 | |
85a9908f |
90 | =item B<create_child_type> |
9ceb576e |
91 | |
d67145ed |
92 | =back |
93 | |
94 | =head1 BUGS |
95 | |
96 | All complex software has bugs lurking in it, and this module is no |
97 | exception. If you find a bug please either email me, or add the bug |
98 | to cpan-RT. |
99 | |
100 | =head1 AUTHOR |
101 | |
102 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
103 | |
104 | =head1 COPYRIGHT AND LICENSE |
105 | |
2840a3b2 |
106 | Copyright 2006-2009 by Infinity Interactive, Inc. |
d67145ed |
107 | |
108 | L<http://www.iinteractive.com> |
109 | |
110 | This library is free software; you can redistribute it and/or modify |
111 | it under the same terms as Perl itself. |
112 | |
113 | =cut |