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