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