Commit | Line | Data |
7e4e1ad4 |
1 | package Moose::Meta::TypeConstraint::Parameterizable; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
9e4ed568 |
7 | our $VERSION = '0.84'; |
75b95414 |
8 | $VERSION = eval $VERSION; |
7e4e1ad4 |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
10 | |
11 | use base 'Moose::Meta::TypeConstraint'; |
90e78884 |
12 | use Moose::Meta::TypeConstraint::Parameterized; |
13 | use Moose::Util::TypeConstraints (); |
7e4e1ad4 |
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) = @_; |
d03bd989 |
22 | |
7e4e1ad4 |
23 | return unless $self->has_constraint_generator; |
d03bd989 |
24 | |
7e4e1ad4 |
25 | return $self->constraint_generator->($type->type_parameter) |
26 | if $type->is_subtype_of($self->name); |
d03bd989 |
27 | |
7e4e1ad4 |
28 | return $self->_can_coerce_constraint_from($type) |
29 | if $self->has_coercion |
30 | && $self->coercion->has_coercion_for_type($type->parent->name); |
d03bd989 |
31 | |
7e4e1ad4 |
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 | |
90e78884 |
45 | sub _parse_type_parameter { |
46 | my ($self, $type_parameter) = @_; |
47 | return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter); |
48 | } |
49 | |
50 | sub parameterize { |
51 | my ($self, $type_parameter) = @_; |
52 | |
53 | my $contained_tc = $self->_parse_type_parameter($type_parameter); |
d03bd989 |
54 | |
0c4c2633 |
55 | ## The type parameter should be a subtype of the parent's type parameter |
56 | ## if there is one. |
d03bd989 |
57 | |
0c4c2633 |
58 | if(my $parent = $self->parent) { |
59 | if($parent->can('type_parameter')) { |
70ea9161 |
60 | unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) { |
61 | require Moose; |
62 | Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter); |
63 | } |
0c4c2633 |
64 | } |
65 | } |
90e78884 |
66 | |
67 | if ( $contained_tc->isa('Moose::Meta::TypeConstraint') ) { |
68 | my $tc_name = $self->name . '[' . $contained_tc->name . ']'; |
69 | return Moose::Meta::TypeConstraint::Parameterized->new( |
70 | name => $tc_name, |
71 | parent => $self, |
72 | type_parameter => $contained_tc, |
73 | ); |
74 | } |
75 | else { |
70ea9161 |
76 | require Moose; |
90e78884 |
77 | Moose->throw_error("The type parameter must be a Moose meta type"); |
78 | } |
79 | } |
80 | |
7e4e1ad4 |
81 | |
82 | 1; |
83 | |
84 | __END__ |
85 | |
86 | |
87 | =pod |
88 | |
89 | =head1 NAME |
90 | |
f6fdcfe7 |
91 | Moose::Meta::TypeConstraint::Parameterizable - Type constraints which can take a parameter (ArrayRef) |
7e4e1ad4 |
92 | |
f6fdcfe7 |
93 | =head1 DESCRIPTION |
7e4e1ad4 |
94 | |
f6fdcfe7 |
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]>. |
7e4e1ad4 |
100 | |
f6fdcfe7 |
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. |
7e4e1ad4 |
104 | |
f6fdcfe7 |
105 | =head1 INHERITANCE |
7e4e1ad4 |
106 | |
f6fdcfe7 |
107 | C<Moose::Meta::TypeConstraint::Parameterizable> is a subclass of |
108 | L<Moose::Meta::TypeConstraint>. |
90e78884 |
109 | |
f6fdcfe7 |
110 | =head1 METHODS |
7e4e1ad4 |
111 | |
f6fdcfe7 |
112 | This class is intentionally not documented because the API is |
113 | confusing and needs some work. |
7e4e1ad4 |
114 | |
115 | =head1 BUGS |
116 | |
d03bd989 |
117 | All complex software has bugs lurking in it, and this module is no |
7e4e1ad4 |
118 | exception. If you find a bug please either email me, or add the bug |
119 | to cpan-RT. |
120 | |
121 | =head1 AUTHOR |
122 | |
123 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
124 | |
125 | =head1 COPYRIGHT AND LICENSE |
126 | |
2840a3b2 |
127 | Copyright 2006-2009 by Infinity Interactive, Inc. |
7e4e1ad4 |
128 | |
129 | L<http://www.iinteractive.com> |
130 | |
131 | This library is free software; you can redistribute it and/or modify |
132 | it under the same terms as Perl itself. |
133 | |
134 | =cut |