All TC objects (except unions) now have inlining code, and tests for all the variatio...
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
CommitLineData
7e4e1ad4 1package Moose::Meta::TypeConstraint::Parameterizable;
2
3use strict;
4use warnings;
5use metaclass;
6
7e4e1ad4 7use base 'Moose::Meta::TypeConstraint';
90e78884 8use Moose::Meta::TypeConstraint::Parameterized;
9use Moose::Util::TypeConstraints ();
7e4e1ad4 10
11__PACKAGE__->meta->add_attribute('constraint_generator' => (
12 accessor => 'constraint_generator',
13 predicate => 'has_constraint_generator',
14));
15
7fb4b360 16__PACKAGE__->meta->add_attribute('inline_generator' => (
17 accessor => 'inline_generator',
18 predicate => 'has_inline_generator',
19));
20
7e4e1ad4 21sub generate_constraint_for {
22 my ($self, $type) = @_;
d03bd989 23
7e4e1ad4 24 return unless $self->has_constraint_generator;
d03bd989 25
7e4e1ad4 26 return $self->constraint_generator->($type->type_parameter)
27 if $type->is_subtype_of($self->name);
d03bd989 28
7e4e1ad4 29 return $self->_can_coerce_constraint_from($type)
30 if $self->has_coercion
31 && $self->coercion->has_coercion_for_type($type->parent->name);
d03bd989 32
7e4e1ad4 33 return;
34}
35
36sub _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
b612a613 46sub generate_inline_for {
47 my ($self, $type, $val) = @_;
48
49 return unless $self->has_inline_generator;
50
964294c1 51 return $self->inline_generator->( $self, $type, $val );
b612a613 52}
53
90e78884 54sub _parse_type_parameter {
55 my ($self, $type_parameter) = @_;
56 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
57}
58
59sub parameterize {
60 my ($self, $type_parameter) = @_;
61
62 my $contained_tc = $self->_parse_type_parameter($type_parameter);
d03bd989 63
0c4c2633 64 ## The type parameter should be a subtype of the parent's type parameter
65 ## if there is one.
d03bd989 66
0c4c2633 67 if(my $parent = $self->parent) {
68 if($parent->can('type_parameter')) {
70ea9161 69 unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) {
70 require Moose;
71 Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter);
72 }
0c4c2633 73 }
74 }
90e78884 75
76 if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
77 my $tc_name = $self->name . '[' . $contained_tc->name . ']';
78 return Moose::Meta::TypeConstraint::Parameterized->new(
7fb4b360 79 name => $tc_name,
80 parent => $self,
81 type_parameter => $contained_tc,
82 parameterized_from => $self,
90e78884 83 );
84 }
85 else {
70ea9161 86 require Moose;
90e78884 87 Moose->throw_error("The type parameter must be a Moose meta type");
88 }
89}
90
7e4e1ad4 91
921;
93
ad46f524 94# ABSTRACT: Type constraints which can take a parameter (ArrayRef)
95
7e4e1ad4 96__END__
97
98
99=pod
100
f6fdcfe7 101=head1 DESCRIPTION
7e4e1ad4 102
f6fdcfe7 103This class represents a parameterizable type constraint. This is a
104type constraint like C<ArrayRef> or C<HashRef>, that can be
105parameterized and made more specific by specifying a contained
106type. For example, instead of just an C<ArrayRef> of anything, you can
107specify that is an C<ArrayRef[Int]>.
7e4e1ad4 108
f6fdcfe7 109A parameterizable constraint should not be used as an attribute type
110constraint. Instead, when parameterized it creates a
111L<Moose::Meta::TypeConstraint::Parameterized> which should be used.
7e4e1ad4 112
f6fdcfe7 113=head1 INHERITANCE
7e4e1ad4 114
f6fdcfe7 115C<Moose::Meta::TypeConstraint::Parameterizable> is a subclass of
116L<Moose::Meta::TypeConstraint>.
90e78884 117
f6fdcfe7 118=head1 METHODS
7e4e1ad4 119
f6fdcfe7 120This class is intentionally not documented because the API is
121confusing and needs some work.
7e4e1ad4 122
123=head1 BUGS
124
d4048ef3 125See L<Moose/BUGS> for details on reporting bugs.
7e4e1ad4 126
7e4e1ad4 127=cut