Use dzil Authority plugin - remove $AUTHORITY from code
[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 use Moose::Meta::TypeConstraint::Parameterizable;
10
11 use base 'Moose::Meta::TypeConstraint';
12
13 __PACKAGE__->meta->add_attribute('type_parameter' => (
14     accessor  => 'type_parameter',
15     predicate => 'has_type_parameter',
16 ));
17
18 sub equals {
19     my ( $self, $type_or_name ) = @_;
20
21     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
22
23     return unless $other->isa(__PACKAGE__);
24
25     return (
26         $self->type_parameter->equals( $other->type_parameter )
27             and
28         $self->parent->equals( $other->parent )
29     );
30 }
31
32 sub compile_type_constraint {
33     my $self = shift;
34
35     unless ( $self->has_type_parameter ) {
36         require Moose;
37         Moose->throw_error("You cannot create a Higher Order type without a type parameter");
38     }
39
40     my $type_parameter = $self->type_parameter;
41
42     unless ( blessed $type_parameter && $type_parameter->isa('Moose::Meta::TypeConstraint') ) {
43         require Moose;
44         Moose->throw_error("The type parameter must be a Moose meta type");
45     }
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     require Moose;
57     Moose->throw_error("The " . $self->name . " constraint cannot be used, because "
58           . $self->parent->name . " doesn't subtype or coerce from a parameterizable type.");
59 }
60
61 sub create_child_type {
62     my ($self, %opts) = @_;
63     return Moose::Meta::TypeConstraint::Parameterizable->new(%opts, parent=>$self);
64 }
65
66 1;
67
68 # ABSTRACT: Type constraints with a bound parameter (ArrayRef[Int])
69
70 __END__
71
72
73 =pod
74
75 =head1 METHODS
76
77 This class is intentionally not documented because the API is
78 confusing and needs some work.
79
80 =head1 INHERITANCE
81
82 C<Moose::Meta::TypeConstraint::Parameterized> is a subclass of
83 L<Moose::Meta::TypeConstraint>.
84
85 =head1 BUGS
86
87 See L<Moose/BUGS> for details on reporting bugs.
88
89 =cut