Use dzil Authority plugin - remove $AUTHORITY from code
[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
16sub generate_constraint_for {
17 my ($self, $type) = @_;
d03bd989 18
7e4e1ad4 19 return unless $self->has_constraint_generator;
d03bd989 20
7e4e1ad4 21 return $self->constraint_generator->($type->type_parameter)
22 if $type->is_subtype_of($self->name);
d03bd989 23
7e4e1ad4 24 return $self->_can_coerce_constraint_from($type)
25 if $self->has_coercion
26 && $self->coercion->has_coercion_for_type($type->parent->name);
d03bd989 27
7e4e1ad4 28 return;
29}
30
31sub _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
90e78884 41sub _parse_type_parameter {
42 my ($self, $type_parameter) = @_;
43 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
44}
45
46sub parameterize {
47 my ($self, $type_parameter) = @_;
48
49 my $contained_tc = $self->_parse_type_parameter($type_parameter);
d03bd989 50
0c4c2633 51 ## The type parameter should be a subtype of the parent's type parameter
52 ## if there is one.
d03bd989 53
0c4c2633 54 if(my $parent = $self->parent) {
55 if($parent->can('type_parameter')) {
70ea9161 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 }
0c4c2633 60 }
61 }
90e78884 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 {
70ea9161 72 require Moose;
90e78884 73 Moose->throw_error("The type parameter must be a Moose meta type");
74 }
75}
76
7e4e1ad4 77
781;
79
ad46f524 80# ABSTRACT: Type constraints which can take a parameter (ArrayRef)
81
7e4e1ad4 82__END__
83
84
85=pod
86
f6fdcfe7 87=head1 DESCRIPTION
7e4e1ad4 88
f6fdcfe7 89This class represents a parameterizable type constraint. This is a
90type constraint like C<ArrayRef> or C<HashRef>, that can be
91parameterized and made more specific by specifying a contained
92type. For example, instead of just an C<ArrayRef> of anything, you can
93specify that is an C<ArrayRef[Int]>.
7e4e1ad4 94
f6fdcfe7 95A parameterizable constraint should not be used as an attribute type
96constraint. Instead, when parameterized it creates a
97L<Moose::Meta::TypeConstraint::Parameterized> which should be used.
7e4e1ad4 98
f6fdcfe7 99=head1 INHERITANCE
7e4e1ad4 100
f6fdcfe7 101C<Moose::Meta::TypeConstraint::Parameterizable> is a subclass of
102L<Moose::Meta::TypeConstraint>.
90e78884 103
f6fdcfe7 104=head1 METHODS
7e4e1ad4 105
f6fdcfe7 106This class is intentionally not documented because the API is
107confusing and needs some work.
7e4e1ad4 108
109=head1 BUGS
110
d4048ef3 111See L<Moose/BUGS> for details on reporting bugs.
7e4e1ad4 112
7e4e1ad4 113=cut