fix is subtype of (sortof)
[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 || [] }) {
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
231sub 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
16aea7bf 266=head2 type_constraints_equals
267
268Checks to see if the internal type contraints are equal.
269
270=cut
271
272sub type_constraints_equals {
179af711 273 my ( $self, $other ) = @_;
274 $self->_type_constraints_op_all($other, "equals");
275}
276
277sub _type_constraints_op_all {
278 my ($self, $other, $op) = @_;
279
280 return unless $other->isa(__PACKAGE__);
281
16aea7bf 282 my @self_type_constraints = @{$self->type_constraints||[]};
283 my @other_type_constraints = @{$other->type_constraints||[]};
179af711 284
285 return unless @self_type_constraints == @other_type_constraints;
286
16aea7bf 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;
179af711 290 my $other_type_constraint = shift @other_type_constraints;
16aea7bf 291
179af711 292 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
293 for $self_type_constraint, $other_type_constraint;
16aea7bf 294
179af711 295 $self_type_constraint->$op($other_type_constraint) or return;
16aea7bf 296 }
297
298 return 1; ##If we get this far, everything is good.
299}
300
179af711 301sub _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
a4a88fef 325=head2 get_message
326
797510e3 327Give you a better peek into what's causing the error. For now we stringify the
328incoming deep value with L<Devel::PartialDump> and pass that on to either your
329custom error message or the default one. In the future we'll try to provide a
330more complete stack trace of the actual offending elements
331
332=cut
333
334around 'get_message' => sub {
335 my ($get_message, $self, $value) = @_;
7559b71f 336 $value = Devel::PartialDump::dump($value)
337 if ref $value;
338 return $self->$get_message($value);
797510e3 339};
a4a88fef 340
a30fa891 341=head1 SEE ALSO
342
343The following modules or resources may be of interest.
344
345L<Moose>, L<Moose::Meta::TypeConstraint>
346
347=head1 AUTHOR
348
349John Napiorkowski, C<< <jjnapiork@cpan.org> >>
350
351=head1 COPYRIGHT & LICENSE
352
353This program is free software; you can redistribute it and/or modify
354it under the same terms as Perl itself.
355
356=cut
357
179af711 358__PACKAGE__->meta->make_immutable;