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