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