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