bump version to 0.56 and update changes for release
[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 Carp         'confess';
9 use Moose::Util::TypeConstraints;
10
11 our $VERSION   = '0.56';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use base 'Moose::Meta::TypeConstraint';
16
17 __PACKAGE__->meta->add_attribute('type_parameter' => (
18     accessor  => 'type_parameter',
19     predicate => 'has_type_parameter',
20 ));
21
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
36 sub compile_type_constraint {
37     my $self = shift;
38     
39     ($self->has_type_parameter)
40         || confess "You cannot create a Higher Order type without a type parameter";
41         
42     my $type_parameter = $self->type_parameter;
43     
44     (blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint'))
45         || confess "The type parameter must be a Moose meta type";
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         }
52     }
53     
54     # if we get here, then we couldn't 
55     # find a way to parameterize this type
56     confess "The " . $self->name . " constraint cannot be used, because " 
57           . $self->parent->name . " doesn't subtype or coerce from a parameterizable type.";
58 }
59
60 1;
61
62 __END__
63
64
65 =pod
66
67 =head1 NAME
68
69 Moose::Meta::TypeConstraint::Parameterized - Higher Order type constraints for Moose
70
71 =head1 METHODS
72
73 =over 4
74
75 =item B<compile_type_constraint>
76
77 =item B<type_parameter>
78
79 =item B<has_type_parameter>
80
81 =item B<meta>
82
83 =item B<equals>
84
85 =back
86
87 =head1 BUGS
88
89 All complex software has bugs lurking in it, and this module is no 
90 exception. If you find a bug please either email me, or add the bug
91 to cpan-RT.
92
93 =head1 AUTHOR
94
95 Stevan Little E<lt>stevan@iinteractive.comE<gt>
96
97 =head1 COPYRIGHT AND LICENSE
98
99 Copyright 2006-2008 by Infinity Interactive, Inc.
100
101 L<http://www.iinteractive.com>
102
103 This library is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself.
105
106 =cut