version bump for 1.9903
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
1 package Moose::Meta::TypeConstraint::Parameterizable;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use base 'Moose::Meta::TypeConstraint';
8 use Moose::Meta::TypeConstraint::Parameterized;
9 use Moose::Util::TypeConstraints ();
10
11 __PACKAGE__->meta->add_attribute('constraint_generator' => (
12     accessor  => 'constraint_generator',
13     predicate => 'has_constraint_generator',
14 ));
15
16 sub generate_constraint_for {
17     my ($self, $type) = @_;
18
19     return unless $self->has_constraint_generator;
20
21     return $self->constraint_generator->($type->type_parameter)
22         if $type->is_subtype_of($self->name);
23
24     return $self->_can_coerce_constraint_from($type)
25         if $self->has_coercion
26         && $self->coercion->has_coercion_for_type($type->parent->name);
27
28     return;
29 }
30
31 sub _can_coerce_constraint_from {
32     my ($self, $type) = @_;
33     my $coercion   = $self->coercion;
34     my $constraint = $self->constraint_generator->($type->type_parameter);
35     return sub {
36         local $_ = $coercion->coerce($_);
37         $constraint->(@_);
38     };
39 }
40
41 sub _parse_type_parameter {
42     my ($self, $type_parameter) = @_;
43     return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
44 }
45
46 sub parameterize {
47     my ($self, $type_parameter) = @_;
48
49     my $contained_tc = $self->_parse_type_parameter($type_parameter);
50
51     ## The type parameter should be a subtype of the parent's type parameter
52     ## if there is one.
53
54     if(my $parent = $self->parent) {
55         if($parent->can('type_parameter')) {
56             unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) {
57                 require Moose;
58                 Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter);
59             }
60         }
61     }
62
63     if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
64         my $tc_name = $self->name . '[' . $contained_tc->name . ']';
65         return Moose::Meta::TypeConstraint::Parameterized->new(
66             name           => $tc_name,
67             parent         => $self,
68             type_parameter => $contained_tc,
69         );
70     }
71     else {
72         require Moose;
73         Moose->throw_error("The type parameter must be a Moose meta type");
74     }
75 }
76
77
78 1;
79
80 # ABSTRACT: Type constraints which can take a parameter (ArrayRef)
81
82 __END__
83
84
85 =pod
86
87 =head1 DESCRIPTION
88
89 This class represents a parameterizable type constraint. This is a
90 type constraint like C<ArrayRef> or C<HashRef>, that can be
91 parameterized and made more specific by specifying a contained
92 type. For example, instead of just an C<ArrayRef> of anything, you can
93 specify that is an C<ArrayRef[Int]>.
94
95 A parameterizable constraint should not be used as an attribute type
96 constraint. Instead, when parameterized it creates a
97 L<Moose::Meta::TypeConstraint::Parameterized> which should be used.
98
99 =head1 INHERITANCE
100
101 C<Moose::Meta::TypeConstraint::Parameterizable> is a subclass of
102 L<Moose::Meta::TypeConstraint>.
103
104 =head1 METHODS
105
106 This class is intentionally not documented because the API is
107 confusing and needs some work.
108
109 =head1 BUGS
110
111 See L<Moose/BUGS> for details on reporting bugs.
112
113 =cut