bump version to 1.14
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterized.pm
CommitLineData
0fbd4b0a 1package Moose::Meta::TypeConstraint::Parameterized;
d67145ed 2
3use strict;
4use warnings;
5use metaclass;
6
7use Scalar::Util 'blessed';
acb8a5db 8use Moose::Util::TypeConstraints;
83526133 9use Moose::Meta::TypeConstraint::Parameterizable;
d67145ed 10
b6cca0d5 11our $VERSION = '1.14';
e606ae5f 12$VERSION = eval $VERSION;
d67145ed 13our $AUTHORITY = 'cpan:STEVAN';
14
15use 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 22sub 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__);
d03bd989 28
dabed765 29 return (
30 $self->type_parameter->equals( $other->type_parameter )
31 and
32 $self->parent->equals( $other->parent )
33 );
34}
35
d67145ed 36sub compile_type_constraint {
37 my $self = shift;
d03bd989 38
70ea9161 39 unless ( $self->has_type_parameter ) {
40 require Moose;
41 Moose->throw_error("You cannot create a Higher Order type without a type parameter");
42 }
43
0fbd4b0a 44 my $type_parameter = $self->type_parameter;
d03bd989 45
70ea9161 46 unless ( blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint') ) {
47 require Moose;
48 Moose->throw_error("The type parameter must be a Moose meta type");
49 }
7e4e1ad4 50
51 foreach my $type (Moose::Util::TypeConstraints::get_all_parameterizable_types()) {
52 if (my $constraint = $type->generate_constraint_for($self)) {
53 $self->_set_constraint($constraint);
d03bd989 54 return $self->SUPER::compile_type_constraint;
7e4e1ad4 55 }
d67145ed 56 }
d03bd989 57
58 # if we get here, then we couldn't
7e4e1ad4 59 # find a way to parameterize this type
70ea9161 60 require Moose;
d03bd989 61 Moose->throw_error("The " . $self->name . " constraint cannot be used, because "
4c0b3599 62 . $self->parent->name . " doesn't subtype or coerce from a parameterizable type.");
d67145ed 63}
64
85a9908f 65sub create_child_type {
9ceb576e 66 my ($self, %opts) = @_;
83526133 67 return Moose::Meta::TypeConstraint::Parameterizable->new(%opts, parent=>$self);
9ceb576e 68}
69
d67145ed 701;
71
72__END__
73
74
75=pod
76
77=head1 NAME
78
f6fdcfe7 79Moose::Meta::TypeConstraint::Parameterized - Type constraints with a bound parameter (ArrayRef[Int])
d67145ed 80
d67145ed 81=head1 METHODS
82
f6fdcfe7 83This class is intentionally not documented because the API is
84confusing and needs some work.
d67145ed 85
f6fdcfe7 86=head1 INHERITANCE
d67145ed 87
f6fdcfe7 88C<Moose::Meta::TypeConstraint::Parameterized> is a subclass of
89L<Moose::Meta::TypeConstraint>.
d67145ed 90
91=head1 BUGS
92
d4048ef3 93See L<Moose/BUGS> for details on reporting bugs.
d67145ed 94
95=head1 AUTHOR
96
97Stevan Little E<lt>stevan@iinteractive.comE<gt>
98
99=head1 COPYRIGHT AND LICENSE
100
7e0492d3 101Copyright 2006-2010 by Infinity Interactive, Inc.
d67145ed 102
103L<http://www.iinteractive.com>
104
105This library is free software; you can redistribute it and/or modify
106it under the same terms as Perl itself.
107
108=cut