bump version to 0.66
[gitmo/Moose.git] / lib / Moose / Meta / TypeConstraint / Parameterizable.pm
CommitLineData
7e4e1ad4 1package Moose::Meta::TypeConstraint::Parameterizable;
2
3use strict;
4use warnings;
5use metaclass;
6
aa7bbf26 7our $VERSION = '0.66';
75b95414 8$VERSION = eval $VERSION;
7e4e1ad4 9our $AUTHORITY = 'cpan:STEVAN';
10
11use base 'Moose::Meta::TypeConstraint';
90e78884 12use Moose::Meta::TypeConstraint::Parameterized;
13use Moose::Util::TypeConstraints ();
7e4e1ad4 14
15__PACKAGE__->meta->add_attribute('constraint_generator' => (
16 accessor => 'constraint_generator',
17 predicate => 'has_constraint_generator',
18));
19
20sub generate_constraint_for {
21 my ($self, $type) = @_;
22
23 return unless $self->has_constraint_generator;
24
25 return $self->constraint_generator->($type->type_parameter)
26 if $type->is_subtype_of($self->name);
27
28 return $self->_can_coerce_constraint_from($type)
29 if $self->has_coercion
30 && $self->coercion->has_coercion_for_type($type->parent->name);
31
32 return;
33}
34
35sub _can_coerce_constraint_from {
36 my ($self, $type) = @_;
37 my $coercion = $self->coercion;
38 my $constraint = $self->constraint_generator->($type->type_parameter);
39 return sub {
40 local $_ = $coercion->coerce($_);
41 $constraint->(@_);
42 };
43}
44
90e78884 45sub _parse_type_parameter {
46 my ($self, $type_parameter) = @_;
47 return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter);
48}
49
50sub parameterize {
51 my ($self, $type_parameter) = @_;
52
53 my $contained_tc = $self->_parse_type_parameter($type_parameter);
0c4c2633 54
55 ## The type parameter should be a subtype of the parent's type parameter
56 ## if there is one.
57
58 if(my $parent = $self->parent) {
59 if($parent->can('type_parameter')) {
60 $contained_tc->is_a_type_of($parent->type_parameter)
61 || Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter);
62 }
63 }
90e78884 64
65 if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
66 my $tc_name = $self->name . '[' . $contained_tc->name . ']';
67 return Moose::Meta::TypeConstraint::Parameterized->new(
68 name => $tc_name,
69 parent => $self,
70 type_parameter => $contained_tc,
71 );
72 }
73 else {
74 Moose->throw_error("The type parameter must be a Moose meta type");
75 }
76}
77
7e4e1ad4 78
791;
80
81__END__
82
83
84=pod
85
86=head1 NAME
87
88Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for Moose
89
90=head1 METHODS
91
92=over 4
93
94=item B<constraint_generator>
95
96=item B<has_constraint_generator>
97
98=item B<generate_constraint_for>
99
90e78884 100=item B<parameterize>
101
53cfaa0e 102Given a single type constraint or type constraint string, this method
103parameterizes the type based on the given argument.
90e78884 104
7e4e1ad4 105=item B<meta>
106
107=back
108
109=head1 BUGS
110
111All complex software has bugs lurking in it, and this module is no
112exception. If you find a bug please either email me, or add the bug
113to cpan-RT.
114
115=head1 AUTHOR
116
117Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119=head1 COPYRIGHT AND LICENSE
120
2840a3b2 121Copyright 2006-2009 by Infinity Interactive, Inc.
7e4e1ad4 122
123L<http://www.iinteractive.com>
124
125This library is free software; you can redistribute it and/or modify
126it under the same terms as Perl itself.
127
128=cut