Commit | Line | Data |
684db121 |
1 | package Mouse::Meta::TypeConstraint; |
bc69ee88 |
2 | use Mouse::Util qw(:meta); # enables strict and warnings |
9c85e9dc |
3 | |
684db121 |
4 | sub new { |
184f8f53 |
5 | my $class = shift; |
6 | my %args = @_ == 1 ? %{$_[0]} : @_; |
f5ee065f |
7 | |
8 | $args{name} = '__ANON__' if !defined $args{name}; |
684db121 |
9 | |
4b58033d |
10 | if(defined $args{parent}) { |
5a592ad7 |
11 | %args = (%{$args{parent}}, %args); |
12 | # a child type must not inherit 'compiled_type_constraint' |
13 | # and 'hand_optimized_type_constraint' from the parent |
14 | delete $args{compiled_type_constraint}; |
15 | delete $args{hand_optimized_type_constraint}; |
16 | } |
17 | |
18 | my $check; |
3b89ea91 |
19 | |
5a592ad7 |
20 | if($check = delete $args{optimized}) { |
3b89ea91 |
21 | $args{hand_optimized_type_constraint} = $check; |
22 | $args{compiled_type_constraint} = $check; |
23 | } |
5a592ad7 |
24 | elsif(my $param = $args{type_parameter}) { |
25 | my $generator = $args{constraint_generator} |
26 | || $class->throw_error("The $args{name} constraint cannot be used," |
27 | . " because $param doesn't subtype from a parameterizable type"); |
28 | # it must be 'constraint' |
29 | $check = $args{constraint} = $generator->($param); |
30 | } |
31 | else { |
32 | $check = $args{constraint}; |
33 | } |
f5ee065f |
34 | |
f5ee065f |
35 | if(defined($check) && ref($check) ne 'CODE'){ |
3a29a080 |
36 | $class->throw_error( |
37 | "Constraint for $args{name} is not a CODE reference"); |
f5ee065f |
38 | } |
39 | |
40 | my $self = bless \%args, $class; |
3a29a080 |
41 | $self->compile_type_constraint() |
5a592ad7 |
42 | if !$args{hand_optimized_type_constraint}; |
f5ee065f |
43 | |
5a592ad7 |
44 | if($args{type_constraints}) { |
45 | $self->_compile_union_type_coercion(); |
46 | } |
f5ee065f |
47 | return $self; |
48 | } |
49 | |
c5940bae |
50 | sub create_child_type { |
f5ee065f |
51 | my $self = shift; |
c5940bae |
52 | return ref($self)->new(@_, parent => $self); |
684db121 |
53 | } |
54 | |
4c99ea95 |
55 | sub name; |
56 | sub parent; |
57 | sub message; |
58 | sub has_coercion; |
2b82d75d |
59 | |
c7576321 |
60 | sub check; |
61 | |
2b82d75d |
62 | sub type_parameter; |
63 | sub __is_parameterized; |
64 | |
4c99ea95 |
65 | sub _compiled_type_constraint; |
66 | sub _compiled_type_coercion; |
67 | |
68 | sub compile_type_constraint; |
69 | |
c7576321 |
70 | |
3a29a080 |
71 | sub _add_type_coercions { # ($self, @pairs) |
ffbbf459 |
72 | my $self = shift; |
73 | |
4b58033d |
74 | if(exists $self->{type_constraints}){ # union type |
75 | $self->throw_error( |
76 | "Cannot add additional type coercions to Union types '$self'"); |
77 | } |
78 | |
cc131889 |
79 | my $coercions = ($self->{coercion_map} ||= []); |
ffbbf459 |
80 | my %has = map{ $_->[0] => undef } @{$coercions}; |
81 | |
82 | for(my $i = 0; $i < @_; $i++){ |
83 | my $from = $_[ $i]; |
84 | my $action = $_[++$i]; |
85 | |
86 | if(exists $has{$from}){ |
c7576321 |
87 | $self->throw_error("A coercion action already exists for '$from'"); |
ffbbf459 |
88 | } |
89 | |
90 | my $type = Mouse::Util::TypeConstraints::find_or_parse_type_constraint($from) |
3a29a080 |
91 | or $self->throw_error( |
92 | "Could not find the type constraint ($from) to coerce from"); |
ffbbf459 |
93 | |
94 | push @{$coercions}, [ $type => $action ]; |
95 | } |
96 | |
4b58033d |
97 | $self->_compile_type_coercion(); |
536e3c33 |
98 | return; |
99 | } |
100 | |
101 | sub _compile_type_coercion { |
102 | my($self) = @_; |
103 | |
cc131889 |
104 | my @coercions = @{$self->{coercion_map}}; |
536e3c33 |
105 | |
106 | $self->{_compiled_type_coercion} = sub { |
107 | my($thing) = @_; |
108 | foreach my $pair (@coercions) { |
109 | #my ($constraint, $converter) = @$pair; |
110 | if ($pair->[0]->check($thing)) { |
111 | local $_ = $thing; |
112 | return $pair->[1]->($thing); |
113 | } |
114 | } |
115 | return $thing; |
116 | }; |
117 | return; |
118 | } |
119 | |
120 | sub _compile_union_type_coercion { |
121 | my($self) = @_; |
122 | |
123 | my @coercions; |
124 | foreach my $type(@{$self->{type_constraints}}){ |
125 | if($type->has_coercion){ |
126 | push @coercions, $type; |
127 | } |
128 | } |
129 | if(@coercions){ |
ffbbf459 |
130 | $self->{_compiled_type_coercion} = sub { |
536e3c33 |
131 | my($thing) = @_; |
132 | foreach my $type(@coercions){ |
133 | my $value = $type->coerce($thing); |
134 | return $value if $self->check($value); |
135 | } |
136 | return $thing; |
ffbbf459 |
137 | }; |
138 | } |
139 | return; |
140 | } |
141 | |
ffbbf459 |
142 | sub coerce { |
143 | my $self = shift; |
c7576321 |
144 | return $_[0] if $self->check(@_); |
ffbbf459 |
145 | |
4b58033d |
146 | my $coercion = $self->{_compiled_type_coercion} |
147 | or $self->throw_error("Cannot coerce without a type coercion"); |
de0d4152 |
148 | return $coercion->(@_); |
feb0e21b |
149 | } |
150 | |
151 | sub get_message { |
152 | my ($self, $value) = @_; |
153 | if ( my $msg = $self->message ) { |
154 | local $_ = $value; |
155 | return $msg->($value); |
156 | } |
157 | else { |
6e647cac |
158 | if(not defined $value) { |
159 | $value = 'undef'; |
160 | } |
161 | elsif( ref($value) && defined(&overload::StrVal) ) { |
162 | $value = overload::StrVal($value); |
163 | } |
537873b0 |
164 | return "Validation failed for '$self' with value $value"; |
feb0e21b |
165 | } |
166 | } |
167 | |
4b58033d |
168 | sub is_a_type_of { |
feb0e21b |
169 | my($self, $other) = @_; |
170 | |
171 | # ->is_a_type_of('__ANON__') is always false |
ca352580 |
172 | return 0 if !ref($other) && $other eq '__ANON__'; |
feb0e21b |
173 | |
174 | (my $other_name = $other) =~ s/\s+//g; |
175 | |
176 | return 1 if $self->name eq $other_name; |
177 | |
178 | if(exists $self->{type_constraints}){ # union |
3a29a080 |
179 | foreach my $type(@{$self->{type_constraints}}) { |
feb0e21b |
180 | return 1 if $type->name eq $other_name; |
181 | } |
182 | } |
183 | |
3a29a080 |
184 | for(my $p = $self->parent; defined $p; $p = $p->parent) { |
185 | return 1 if $p->name eq $other_name; |
feb0e21b |
186 | } |
187 | |
188 | return 0; |
189 | } |
190 | |
b4d791ba |
191 | # See also Moose::Meta::TypeConstraint::Parameterizable |
192 | sub parameterize{ |
193 | my($self, $param, $name) = @_; |
194 | |
195 | if(!ref $param){ |
196 | require Mouse::Util::TypeConstraints; |
197 | $param = Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($param); |
198 | } |
199 | |
200 | $name ||= sprintf '%s[%s]', $self->name, $param->name; |
b4d791ba |
201 | return Mouse::Meta::TypeConstraint->new( |
fc83f4cf |
202 | name => $name, |
203 | parent => $self, |
204 | type_parameter => $param, |
b4d791ba |
205 | ); |
206 | } |
feb0e21b |
207 | |
78508064 |
208 | sub assert_valid { |
209 | my ($self, $value) = @_; |
210 | |
c7576321 |
211 | if(!$self->check($value)){ |
212 | $self->throw_error($self->get_message($value)); |
78508064 |
213 | } |
214 | return 1; |
215 | } |
216 | |
6e647cac |
217 | sub _as_string { $_[0]->name } # overload "" |
f6c81f00 |
218 | sub _identity; # overload 0+ |
6e647cac |
219 | |
220 | sub _unite { # overload infix:<|> |
221 | my($lhs, $rhs) = @_; |
222 | require Mouse::Util::TypeConstraints; |
223 | return Mouse::Util::TypeConstraints::find_or_parse_type_constraint( |
224 | " $lhs | $rhs", |
225 | ); |
226 | } |
227 | |
684db121 |
228 | 1; |
229 | __END__ |
230 | |
231 | =head1 NAME |
232 | |
1820fffe |
233 | Mouse::Meta::TypeConstraint - The Mouse Type Constraint metaclass |
684db121 |
234 | |
a25ca8d6 |
235 | =head1 VERSION |
236 | |
825f7cda |
237 | This document describes Mouse version 0.74 |
a25ca8d6 |
238 | |
684db121 |
239 | =head1 DESCRIPTION |
240 | |
3a29a080 |
241 | This class represents a type constraint, including built-in |
242 | type constraints, union type constraints, parameterizable/ |
243 | parameterized type constraints, as well as custom type |
244 | constraints |
684db121 |
245 | |
246 | =head1 METHODS |
247 | |
ba05bb84 |
248 | =over |
3a29a080 |
249 | |
250 | =item C<< Mouse::Meta::TypeConstraint->new(%options) >> |
251 | |
252 | =item C<< $constraint->name >> |
253 | |
254 | =item C<< $constraint->parent >> |
255 | |
256 | =item C<< $constraint->constraint >> |
257 | |
258 | =item C<< $constraint->has_coercion >> |
259 | |
260 | =item C<< $constraint->message >> |
261 | |
262 | =item C<< $constraint->is_a_subtype_of($name or $object) >> |
263 | |
264 | =item C<< $constraint->coerce($value) >> |
265 | |
266 | =item C<< $constraint->check($value) >> |
267 | |
268 | =item C<< $constraint->assert_valid($value) >> |
684db121 |
269 | |
3a29a080 |
270 | =item C<< $constraint->get_message($value) >> |
684db121 |
271 | |
3a29a080 |
272 | =item C<< $constraint->create_child_type(%options) >> |
684db121 |
273 | |
274 | =back |
275 | |
1820fffe |
276 | =head1 SEE ALSO |
277 | |
278 | L<Moose::Meta::TypeConstraint> |
279 | |
684db121 |
280 | =cut |
281 | |