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