Commit | Line | Data |
7e4e1ad4 |
1 | package Moose::Meta::TypeConstraint::Parameterizable; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use metaclass; |
6 | |
7e4e1ad4 |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | use base 'Moose::Meta::TypeConstraint'; |
90e78884 |
10 | use Moose::Meta::TypeConstraint::Parameterized; |
11 | use Moose::Util::TypeConstraints (); |
7e4e1ad4 |
12 | |
13 | __PACKAGE__->meta->add_attribute('constraint_generator' => ( |
14 | accessor => 'constraint_generator', |
15 | predicate => 'has_constraint_generator', |
16 | )); |
17 | |
18 | sub generate_constraint_for { |
19 | my ($self, $type) = @_; |
d03bd989 |
20 | |
7e4e1ad4 |
21 | return unless $self->has_constraint_generator; |
d03bd989 |
22 | |
7e4e1ad4 |
23 | return $self->constraint_generator->($type->type_parameter) |
24 | if $type->is_subtype_of($self->name); |
d03bd989 |
25 | |
7e4e1ad4 |
26 | return $self->_can_coerce_constraint_from($type) |
27 | if $self->has_coercion |
28 | && $self->coercion->has_coercion_for_type($type->parent->name); |
d03bd989 |
29 | |
7e4e1ad4 |
30 | return; |
31 | } |
32 | |
33 | sub _can_coerce_constraint_from { |
34 | my ($self, $type) = @_; |
35 | my $coercion = $self->coercion; |
36 | my $constraint = $self->constraint_generator->($type->type_parameter); |
37 | return sub { |
38 | local $_ = $coercion->coerce($_); |
39 | $constraint->(@_); |
40 | }; |
41 | } |
42 | |
90e78884 |
43 | sub _parse_type_parameter { |
44 | my ($self, $type_parameter) = @_; |
45 | return Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($type_parameter); |
46 | } |
47 | |
48 | sub parameterize { |
49 | my ($self, $type_parameter) = @_; |
50 | |
51 | my $contained_tc = $self->_parse_type_parameter($type_parameter); |
d03bd989 |
52 | |
0c4c2633 |
53 | ## The type parameter should be a subtype of the parent's type parameter |
54 | ## if there is one. |
d03bd989 |
55 | |
0c4c2633 |
56 | if(my $parent = $self->parent) { |
57 | if($parent->can('type_parameter')) { |
70ea9161 |
58 | unless ( $contained_tc->is_a_type_of($parent->type_parameter) ) { |
59 | require Moose; |
60 | Moose->throw_error("$type_parameter is not a subtype of ".$parent->type_parameter); |
61 | } |
0c4c2633 |
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 { |
70ea9161 |
74 | require Moose; |
90e78884 |
75 | Moose->throw_error("The type parameter must be a Moose meta type"); |
76 | } |
77 | } |
78 | |
7e4e1ad4 |
79 | |
80 | 1; |
81 | |
ad46f524 |
82 | # ABSTRACT: Type constraints which can take a parameter (ArrayRef) |
83 | |
7e4e1ad4 |
84 | __END__ |
85 | |
86 | |
87 | =pod |
88 | |
f6fdcfe7 |
89 | =head1 DESCRIPTION |
7e4e1ad4 |
90 | |
f6fdcfe7 |
91 | This class represents a parameterizable type constraint. This is a |
92 | type constraint like C<ArrayRef> or C<HashRef>, that can be |
93 | parameterized and made more specific by specifying a contained |
94 | type. For example, instead of just an C<ArrayRef> of anything, you can |
95 | specify that is an C<ArrayRef[Int]>. |
7e4e1ad4 |
96 | |
f6fdcfe7 |
97 | A parameterizable constraint should not be used as an attribute type |
98 | constraint. Instead, when parameterized it creates a |
99 | L<Moose::Meta::TypeConstraint::Parameterized> which should be used. |
7e4e1ad4 |
100 | |
f6fdcfe7 |
101 | =head1 INHERITANCE |
7e4e1ad4 |
102 | |
f6fdcfe7 |
103 | C<Moose::Meta::TypeConstraint::Parameterizable> is a subclass of |
104 | L<Moose::Meta::TypeConstraint>. |
90e78884 |
105 | |
f6fdcfe7 |
106 | =head1 METHODS |
7e4e1ad4 |
107 | |
f6fdcfe7 |
108 | This class is intentionally not documented because the API is |
109 | confusing and needs some work. |
7e4e1ad4 |
110 | |
111 | =head1 BUGS |
112 | |
d4048ef3 |
113 | See L<Moose/BUGS> for details on reporting bugs. |
7e4e1ad4 |
114 | |
7e4e1ad4 |
115 | =cut |