fix for bug and prep for cpan
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured.pm
1 package ## Hide from PAUSE
2  MooseX::Meta::TypeConstraint::Structured;
3 # ABSTRACT: MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
4
5 use Moose;
6 use Devel::PartialDump;
7 use Moose::Util::TypeConstraints ();
8 use MooseX::Meta::TypeCoercion::Structured;
9 extends 'Moose::Meta::TypeConstraint';
10
11
12 =head1 DESCRIPTION
13
14 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
15 such a way as that they are all applied to an incoming list of arguments.  The
16 idea here is that a Type Constraint could be something like, "An Int followed by
17 an Int and then a Str" and that this could be done so with a declaration like:
18
19     Tuple[Int,Int,Str]; ## Example syntax
20
21 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
22 example) which are intended to function together.
23
24 =attr type_constraints
25
26 A list of L<Moose::Meta::TypeConstraint> objects.
27
28 =cut
29
30 has 'type_constraints' => (
31     is=>'ro',
32     isa=>'Ref',
33     predicate=>'has_type_constraints',
34 );
35
36 =attr constraint_generator
37
38 A subref or closure that contains the way we validate incoming values against
39 a set of type constraints.
40
41 =cut
42
43 has 'constraint_generator' => (
44     is=>'ro',
45     isa=>'CodeRef',
46     predicate=>'has_constraint_generator',
47 );
48
49 has coercion => (
50     is      => 'ro',
51     isa     => 'Object',
52     builder => '_build_coercion',
53 );
54
55 sub _build_coercion {
56     my ($self) = @_;
57     return MooseX::Meta::TypeCoercion::Structured->new(
58         type_constraint => $self,
59     );
60 }
61
62 =method validate
63
64 Messing with validate so that we can support niced error messages.
65
66 =cut
67
68 sub _clean_message {
69     my $message = shift @_;
70     $message =~s/MooseX::Types::Structured:://g;
71     return $message;
72 }
73
74 override 'validate' => sub {
75     my ($self, $value, $message_stack) = @_;
76     unless ($message_stack) {
77         $message_stack = MooseX::Types::Structured::MessageStack->new();
78     }
79
80     $message_stack->inc_level;
81
82     if ($self->_compiled_type_constraint->($value, $message_stack)) {
83         ## Everything is good, no error message to return
84         return undef;
85     } else {
86         ## Whoops, need to figure out the right error message
87         my $args = Devel::PartialDump::dump($value);
88         $message_stack->dec_level;
89         if($message_stack->has_messages) {
90             if($message_stack->level) {
91                 ## we are inside a deeply structured constraint
92                 return $self->get_message($args);
93             } else {
94                 my $message_str = $message_stack->as_string;
95                 return _clean_message($self->get_message("$args, Internal Validation Error is: $message_str"));
96             }
97         } else {
98             return $self->get_message($args);
99         }
100     }
101 };
102
103 =method generate_constraint_for ($type_constraints)
104
105 Given some type constraints, use them to generate validation rules for an ref
106 of values (to be passed at check time)
107
108 =cut
109
110 sub generate_constraint_for {
111     my ($self, $type_constraints) = @_;
112     return $self->constraint_generator->($self, $type_constraints);
113 }
114
115 =method parameterize (@type_constraints)
116
117 Given a ref of type constraints, create a structured type.
118
119 =cut
120
121 sub parameterize {
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 =method __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 =method 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 =method 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 =method is_a_type_of
189
190 =method is_subtype_of
191
192 =method 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       or return;
202
203     return unless $other->isa(__PACKAGE__);
204
205     return (
206         $self->parent->equals($other->parent)
207             and
208         $self->type_constraints_equals($other)
209     );
210 }
211
212 sub is_a_type_of {
213     my ( $self, $type_or_name ) = @_;
214     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
215       or return;
216
217     if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
218         if ( $self->parent->is_a_type_of($other->parent) ) {
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     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name)
234       or return;
235     if ( $other->isa(__PACKAGE__) ) {
236         if ( $other->type_constraints and $self->type_constraints ) {
237             if ( $self->parent->is_a_type_of($other->parent) ) {
238                 return (
239                     $self->_type_constraints_op_all($other, "is_a_type_of")
240                       and
241                     $self->_type_constraints_op_any($other, "is_subtype_of")
242                 );
243             } elsif ( $self->parent->is_a_type_of($other) ) {
244                 return 1;
245                 # FIXME compare?
246             } else {
247                 return 0;
248             }
249         } else {
250             if ( $self->type_constraints ) {
251                 if ( $self->SUPER::is_subtype_of($other) ) {
252                     return 1;
253                 } else {
254                     return;
255                 }
256             } else {
257                 return $self->parent->is_subtype_of($other->parent);
258             }
259         }
260     } else {
261         return $self->SUPER::is_subtype_of($other);
262     }
263 }
264
265 =method type_constraints_equals
266
267 Checks to see if the internal type constraints are equal.
268
269 =cut
270
271 sub type_constraints_equals {
272     my ( $self, $other ) = @_;
273     $self->_type_constraints_op_all($other, "equals");
274 }
275
276 sub _type_constraints_op_all {
277     my ($self, $other, $op) = @_;
278
279     return unless $other->isa(__PACKAGE__);
280
281     my @self_type_constraints = @{$self->type_constraints||[]};
282     my @other_type_constraints = @{$other->type_constraints||[]};
283
284     return unless @self_type_constraints == @other_type_constraints;
285
286     ## Incoming ay be either arrayref or hashref, need top compare both
287     while(@self_type_constraints) {
288         my $self_type_constraint = shift @self_type_constraints;
289         my $other_type_constraint = shift @other_type_constraints;
290
291         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
292           for $self_type_constraint, $other_type_constraint;
293
294         my $result = $self_type_constraint->$op($other_type_constraint);
295         return unless $result;
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 =method 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 =cut
348
349 __PACKAGE__->meta->make_immutable(inline_constructor => 0);