update dist.ini
[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 sub {
113         my $arg =  shift @_;
114         my $constraint_generator = $self->constraint_generator;
115         my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
116         return $result;
117     };
118 }
119
120 =method parameterize (@type_constraints)
121
122 Given a ref of type constraints, create a structured type.
123
124 =cut
125
126 sub parameterize {
127     my ($self, @type_constraints) = @_;
128     my $class = ref $self;
129     my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
130     my $constraint_generator = $self->__infer_constraint_generator;
131
132     return $class->new(
133         name => $name,
134         parent => $self,
135         type_constraints => \@type_constraints,
136         constraint_generator => $constraint_generator,
137     );
138 }
139
140 =method __infer_constraint_generator
141
142 This returns a CODEREF which generates a suitable constraint generator.  Not
143 user servicable, you'll never call this directly.
144
145 =cut
146
147 sub __infer_constraint_generator {
148     my ($self) = @_;
149     if($self->has_constraint_generator) {
150         return $self->constraint_generator;
151     } else {
152         return sub {
153             ## I'm not sure about this stuff but everything seems to work
154             my $tc = shift @_;
155             my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
156             $self->constraint->($merged_tc, @_);
157         };
158     }
159 }
160
161 =method compile_type_constraint
162
163 hook into compile_type_constraint so we can set the correct validation rules.
164
165 =cut
166
167 around 'compile_type_constraint' => sub {
168     my ($compile_type_constraint, $self, @args) = @_;
169
170     if($self->has_type_constraints) {
171         my $type_constraints = $self->type_constraints;
172         my $constraint = $self->generate_constraint_for($type_constraints);
173         $self->_set_constraint($constraint);
174     }
175
176     return $self->$compile_type_constraint(@args);
177 };
178
179 =method create_child_type
180
181 modifier to make sure we get the constraint_generator
182
183 =cut
184
185 around 'create_child_type' => sub {
186     my ($create_child_type, $self, %opts) = @_;
187     return $self->$create_child_type(
188         %opts,
189         constraint_generator => $self->__infer_constraint_generator,
190     );
191 };
192
193 =method is_a_type_of
194
195 =method is_subtype_of
196
197 =method equals
198
199 Override the base class behavior.
200
201 =cut
202
203 sub equals {
204     my ( $self, $type_or_name ) = @_;
205     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
206
207     return unless $other->isa(__PACKAGE__);
208
209     return (
210         $self->parent->equals($other->parent)
211             and
212         $self->type_constraints_equals($other)
213     );
214 }
215
216 sub is_a_type_of {
217     my ( $self, $type_or_name ) = @_;
218     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
219
220     if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
221         if ( $self->parent->is_a_type_of($other->parent) ) {
222             return $self->_type_constraints_op_all($other, "is_a_type_of");
223         } elsif ( $self->parent->is_a_type_of($other) ) {
224             return 1;
225             # FIXME compare?
226         } else {
227             return 0;
228         }
229     } else {
230         return $self->SUPER::is_a_type_of($other);
231     }
232 }
233
234 sub is_subtype_of {
235     my ( $self, $type_or_name ) = @_;
236     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
237     if ( $other->isa(__PACKAGE__) ) {
238         if ( $other->type_constraints and $self->type_constraints ) {
239             if ( $self->parent->is_a_type_of($other->parent) ) {
240                 return (
241                     $self->_type_constraints_op_all($other, "is_a_type_of")
242                       and
243                     $self->_type_constraints_op_any($other, "is_subtype_of")
244                 );
245             } elsif ( $self->parent->is_a_type_of($other) ) {
246                 return 1;
247                 # FIXME compare?
248             } else {
249                 return 0;
250             }
251         } else {
252             if ( $self->type_constraints ) {
253                 if ( $self->SUPER::is_subtype_of($other) ) {
254                     return 1;
255                 } else {
256                     return;
257                 }
258             } else {
259                 return $self->parent->is_subtype_of($other->parent);
260             }
261         }
262     } else {
263         return $self->SUPER::is_subtype_of($other);
264     }
265 }
266
267 =method type_constraints_equals
268
269 Checks to see if the internal type constraints are equal.
270
271 =cut
272
273 sub type_constraints_equals {
274     my ( $self, $other ) = @_;
275     $self->_type_constraints_op_all($other, "equals");
276 }
277
278 sub _type_constraints_op_all {
279     my ($self, $other, $op) = @_;
280
281     return unless $other->isa(__PACKAGE__);
282
283     my @self_type_constraints = @{$self->type_constraints||[]};
284     my @other_type_constraints = @{$other->type_constraints||[]};
285
286     return unless @self_type_constraints == @other_type_constraints;
287
288     ## Incoming ay be either arrayref or hashref, need top compare both
289     while(@self_type_constraints) {
290         my $self_type_constraint = shift @self_type_constraints;
291         my $other_type_constraint = shift @other_type_constraints;
292
293         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
294           for $self_type_constraint, $other_type_constraint;
295
296         my $result = $self_type_constraint->$op($other_type_constraint);
297         return unless $result;
298     }
299
300     return 1; ##If we get this far, everything is good.
301 }
302
303 sub _type_constraints_op_any {
304     my ($self, $other, $op) = @_;
305
306     return unless $other->isa(__PACKAGE__);
307
308     my @self_type_constraints = @{$self->type_constraints||[]};
309     my @other_type_constraints = @{$other->type_constraints||[]};
310
311     return unless @self_type_constraints == @other_type_constraints;
312
313     ## Incoming ay be either arrayref or hashref, need top compare both
314     while(@self_type_constraints) {
315         my $self_type_constraint = shift @self_type_constraints;
316         my $other_type_constraint = shift @other_type_constraints;
317
318         $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
319           for $self_type_constraint, $other_type_constraint;
320
321         return 1 if $self_type_constraint->$op($other_type_constraint);
322     }
323
324     return 0;
325 }
326
327 =method get_message
328
329 Give you a better peek into what's causing the error.  For now we stringify the
330 incoming deep value with L<Devel::PartialDump> and pass that on to either your
331 custom error message or the default one.  In the future we'll try to provide a
332 more complete stack trace of the actual offending elements
333
334 =cut
335
336 around 'get_message' => sub {
337     my ($get_message, $self, $value) = @_;
338     $value = Devel::PartialDump::dump($value)
339      if ref $value;
340     return $self->$get_message($value);
341 };
342
343 =head1 SEE ALSO
344
345 The following modules or resources may be of interest.
346
347 L<Moose>, L<Moose::Meta::TypeConstraint>
348
349 =cut
350
351 __PACKAGE__->meta->make_immutable(inline_constructor => 0);