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