fix is subtype of (sortof)
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured.pm
1 package ## Hide from PAUSE
2  MooseX::Meta::TypeConstraint::Structured;
3
4 use Moose;
5 use Devel::PartialDump;
6 use Moose::Util::TypeConstraints ();
7 use MooseX::Meta::TypeCoercion::Structured;
8 extends 'Moose::Meta::TypeConstraint';
9
10 =head1 NAME
11
12 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
13
14 =head1 DESCRIPTION
15
16 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
17 such a way as that they are all applied to an incoming list of arguments.  The
18 idea here is that a Type Constraint could be something like, "An Int followed by
19 an Int and then a Str" and that this could be done so with a declaration like:
20
21     Tuple[Int,Int,Str]; ## Example syntax
22     
23 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
24 example) which are intended to function together.
25
26 =head1 ATTRIBUTES
27
28 This class defines the following attributes.
29
30 =head2 type_constraints
31
32 A list of L<Moose::Meta::TypeConstraint> objects.
33
34 =cut
35
36 has 'type_constraints' => (
37     is=>'ro',
38     isa=>'Ref',
39     predicate=>'has_type_constraints',
40 );
41
42 =head2 constraint_generator
43
44 A subref or closure that contains the way we validate incoming values against
45 a set of type constraints.
46
47 =cut
48
49 has 'constraint_generator' => (
50     is=>'ro',
51     isa=>'CodeRef',
52     predicate=>'has_constraint_generator',
53 );
54
55 =head1 METHODS
56
57 This class defines the following methods.
58
59 =head2 new
60
61 Initialization stuff.
62
63 =cut
64
65 around 'new' => sub {
66     my ($new, $class, @args)  = @_;
67     my $self = $class->$new(@args);
68     $self->coercion(MooseX::Meta::TypeCoercion::Structured->new(
69         type_constraint => $self,
70     ));
71     return $self;
72 };
73
74 =head2 validate
75
76 Messing with validate so that we can support niced error messages.
77 =cut
78
79 override 'validate' => sub {
80     my ($self, @args) = @_;
81     my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
82
83     if ($self->_compiled_type_constraint->(@args, $message)) {
84         ## Everything is good, no error message to return
85         return undef;
86     } else {
87         ## Whoops, need to figure out the right error message
88         my $args = Devel::PartialDump::dump(@args);
89         if(my $message = $message->{message}) {
90             return $self->get_message("$args, Internal Validation Error is: $message");
91         } else {
92             return $self->get_message($args);
93         }
94     }
95 };
96
97 =head2 generate_constraint_for ($type_constraints)
98
99 Given some type constraints, use them to generate validation rules for an ref
100 of values (to be passed at check time)
101
102 =cut
103
104 sub generate_constraint_for {
105     my ($self, $type_constraints) = @_;
106     return sub {
107         my $arg =  shift @_;
108         my $constraint_generator = $self->constraint_generator;
109         my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
110         return $result;
111     };
112 }
113
114 =head2 parameterize (@type_constraints)
115
116 Given a ref of type constraints, create a structured type.
117
118 =cut
119
120 sub parameterize {
121     
122     my ($self, @type_constraints) = @_;
123     my $class = ref $self;
124     my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
125     my $constraint_generator = $self->__infer_constraint_generator;
126
127     return $class->new(
128         name => $name,
129         parent => $self,
130         type_constraints => \@type_constraints,
131         constraint_generator => $constraint_generator,
132     );
133 }
134
135 =head2 __infer_constraint_generator
136
137 This returns a CODEREF which generates a suitable constraint generator.  Not
138 user servicable, you'll never call this directly.
139
140 =cut
141
142 sub __infer_constraint_generator {
143     my ($self) = @_;
144     if($self->has_constraint_generator) {
145         return $self->constraint_generator;
146     } else {
147         return sub {
148             ## I'm not sure about this stuff but everything seems to work
149             my $tc = shift @_;
150             my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
151             $self->constraint->($merged_tc, @_);            
152         };
153     }    
154 }
155
156 =head2 compile_type_constraint
157
158 hook into compile_type_constraint so we can set the correct validation rules.
159
160 =cut
161
162 around 'compile_type_constraint' => sub {
163     my ($compile_type_constraint, $self, @args) = @_;
164     
165     if($self->has_type_constraints) {
166         my $type_constraints = $self->type_constraints;
167         my $constraint = $self->generate_constraint_for($type_constraints);
168         $self->_set_constraint($constraint);        
169     }
170
171     return $self->$compile_type_constraint(@args);
172 };
173
174 =head2 create_child_type
175
176 modifier to make sure we get the constraint_generator
177
178 =cut
179
180 around 'create_child_type' => sub {
181     my ($create_child_type, $self, %opts) = @_;
182     return $self->$create_child_type(
183         %opts,
184         constraint_generator => $self->__infer_constraint_generator,
185     );
186 };
187
188 =head2 is_a_type_of
189
190 =head2 is_subtype_of
191
192 =head2 equals
193
194 Override the base class behavior.
195
196 =cut
197
198 sub equals {
199     my ( $self, $type_or_name ) = @_;
200     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
201
202     return unless $other->isa(__PACKAGE__);
203     
204     return (
205         $self->parent->equals($other->parent)
206             and
207         $self->type_constraints_equals($other)
208     );
209 }
210
211 sub is_a_type_of {
212     my ( $self, $type_or_name ) = @_;
213     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
214
215     if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
216         warn "structured ( $self, $other )";
217         if ( $self->parent->is_a_type_of($other->parent) ) {
218             warn "related ( $self, $other )";
219             return $self->_type_constraints_op_all($other, "is_a_type_of");
220         } elsif ( $self->parent->is_a_type_of($other) ) {
221             return 1;
222             # FIXME compare?
223         } else {
224             return 0;
225         }
226     } else {
227         return $self->SUPER::is_a_type_of($other);
228     }
229 }
230
231 sub is_subtype_of {
232     my ( $self, $type_or_name ) = @_;
233
234     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
235
236     if ( $other->isa(__PACKAGE__) ) {
237         if ( $other->type_constraints and $self->type_constraints ) {
238             if ( $self->parent->is_a_type_of($other->parent) ) {
239                 return (
240                     $self->_type_constraints_op_all($other, "is_a_type_of")
241                       and
242                     $self->_type_constraints_op_any($other, "is_subtype_of")
243                 );
244             } elsif ( $self->parent->is_a_type_of($other) ) {
245                 return 1;
246                 # FIXME compare?
247             } else {
248                 return 0;
249             }
250         } else {
251             if ( $self->type_constraints ) {
252                 if ( $self->SUPER::is_subtype_of($other) ) {
253                     return 1;
254                 } else {
255                     return;
256                 }
257             } else {
258                 return $self->parent->is_subtype_of($other->parent);
259             }
260         }
261     } else {
262         return $self->SUPER::is_subtype_of($other);
263     }
264 }
265
266 =head2 type_constraints_equals
267
268 Checks to see if the internal type contraints are equal.
269
270 =cut
271
272 sub type_constraints_equals {
273     my ( $self, $other ) = @_;
274     $self->_type_constraints_op_all($other, "equals");
275 }
276
277 sub _type_constraints_op_all {
278     my ($self, $other, $op) = @_;
279
280     return unless $other->isa(__PACKAGE__);
281
282     my @self_type_constraints = @{$self->type_constraints||[]};
283     my @other_type_constraints = @{$other->type_constraints||[]};
284
285     return unless @self_type_constraints == @other_type_constraints;
286
287     ## Incoming ay be either arrayref or hashref, need top compare both
288     while(@self_type_constraints) {
289         my $self_type_constraint = shift @self_type_constraints;
290         my $other_type_constraint = shift @other_type_constraints;
291         
292         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
293           for $self_type_constraint, $other_type_constraint;
294
295         $self_type_constraint->$op($other_type_constraint) or return;
296     }
297     
298     return 1; ##If we get this far, everything is good.
299 }
300
301 sub _type_constraints_op_any {
302     my ($self, $other, $op) = @_;
303
304     return unless $other->isa(__PACKAGE__);
305
306     my @self_type_constraints = @{$self->type_constraints||[]};
307     my @other_type_constraints = @{$other->type_constraints||[]};
308
309     return unless @self_type_constraints == @other_type_constraints;
310
311     ## Incoming ay be either arrayref or hashref, need top compare both
312     while(@self_type_constraints) {
313         my $self_type_constraint = shift @self_type_constraints;
314         my $other_type_constraint = shift @other_type_constraints;
315         
316         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
317           for $self_type_constraint, $other_type_constraint;
318         
319         return 1 if $self_type_constraint->$op($other_type_constraint);
320     }
321
322     return 0;
323 }
324
325 =head2 get_message
326
327 Give you a better peek into what's causing the error.  For now we stringify the
328 incoming deep value with L<Devel::PartialDump> and pass that on to either your
329 custom error message or the default one.  In the future we'll try to provide a
330 more complete stack trace of the actual offending elements
331
332 =cut
333
334 around 'get_message' => sub {
335     my ($get_message, $self, $value) = @_;
336     $value = Devel::PartialDump::dump($value)
337      if ref $value;
338     return $self->$get_message($value);
339 };
340
341 =head1 SEE ALSO
342
343 The following modules or resources may be of interest.
344
345 L<Moose>, L<Moose::Meta::TypeConstraint>
346
347 =head1 AUTHOR
348
349 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
350
351 =head1 COPYRIGHT & LICENSE
352
353 This program is free software; you can redistribute it and/or modify
354 it under the same terms as Perl itself.
355
356 =cut
357
358 __PACKAGE__->meta->make_immutable;