176e52f0b15c30d3622022de6e76c6678e405f52
[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 our $VERSION   = '0.57';
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
10
11 use base 'Moose::Meta::TypeConstraint';
12 use Moose::Meta::TypeConstraint::Parameterized;
13 use Moose::Util::TypeConstraints ();
14
15 __PACKAGE__->meta->add_attribute('constraint_generator' => (
16     accessor  => 'constraint_generator',
17     predicate => 'has_constraint_generator',
18 ));
19
20 sub 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
35 sub _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
45 sub parse_parameter_str {
46     my ($self, $type_str) = @_;
47     return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_str);
48 }
49
50 sub parameterize {
51     my ( $self, $contained_tc ) = @_;
52
53     if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) {
54         my $tc_name = $self->name . '[' . $contained_tc->name . ']';
55         return Moose::Meta::TypeConstraint::Parameterized->new(
56             name           => $tc_name,
57             parent         => $self,
58             type_parameter => $contained_tc,
59         );
60     }
61     else {
62         Moose->throw_error("The type parameter must be a Moose meta type");
63     }
64 }
65
66
67 1;
68
69 __END__
70
71
72 =pod
73
74 =head1 NAME
75
76 Moose::Meta::TypeConstraint::Parameterizable - Higher Order type constraints for Moose
77
78 =head1 METHODS
79
80 =over 4
81
82 =item B<constraint_generator>
83
84 =item B<has_constraint_generator>
85
86 =item B<generate_constraint_for>
87
88 =item B<parse_parameter_str>
89
90 Given a string, convert it to a Perl structure.
91
92 =item B<parameterize>
93
94 Given an array of type constraints, parameterize the current type constraint.
95
96 =item B<meta>
97
98 =back
99
100 =head1 BUGS
101
102 All complex software has bugs lurking in it, and this module is no 
103 exception. If you find a bug please either email me, or add the bug
104 to cpan-RT.
105
106 =head1 AUTHOR
107
108 Stevan Little E<lt>stevan@iinteractive.comE<gt>
109
110 =head1 COPYRIGHT AND LICENSE
111
112 Copyright 2006-2008 by Infinity Interactive, Inc.
113
114 L<http://www.iinteractive.com>
115
116 This library is free software; you can redistribute it and/or modify
117 it under the same terms as Perl itself.
118
119 =cut