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