fixed incorrect alway finding an error with TC->validate
[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 (
205 $self->type_constraints_equals($other)
206 and
207 $self->parent->equals( $other->parent )
208 );
209}
210
211=head2 type_constraints_equals
212
213Checks to see if the internal type contraints are equal.
214
215=cut
216
217sub type_constraints_equals {
218 my ($self, $other) = @_;
219 my @self_type_constraints = @{$self->type_constraints||[]};
220 my @other_type_constraints = @{$other->type_constraints||[]};
221
222 ## Incoming ay be either arrayref or hashref, need top compare both
223 while(@self_type_constraints) {
224 my $self_type_constraint = shift @self_type_constraints;
225 my $other_type_constraint = shift @other_type_constraints
226 || return; ## $other needs the same number of children.
227
228 if( ref $self_type_constraint) {
229 $self_type_constraint->equals($other_type_constraint)
230 || return; ## type constraints obviously need top be equal
231 } else {
232 $self_type_constraint eq $other_type_constraint
233 || return; ## strings should be equal
234 }
235
236 }
237
238 return 1; ##If we get this far, everything is good.
239}
240
a4a88fef 241=head2 get_message
242
797510e3 243Give you a better peek into what's causing the error. For now we stringify the
244incoming deep value with L<Devel::PartialDump> and pass that on to either your
245custom error message or the default one. In the future we'll try to provide a
246more complete stack trace of the actual offending elements
247
248=cut
249
250around 'get_message' => sub {
251 my ($get_message, $self, $value) = @_;
7559b71f 252 $value = Devel::PartialDump::dump($value)
253 if ref $value;
254 return $self->$get_message($value);
797510e3 255};
a4a88fef 256
a30fa891 257=head1 SEE ALSO
258
259The following modules or resources may be of interest.
260
261L<Moose>, L<Moose::Meta::TypeConstraint>
262
263=head1 AUTHOR
264
265John Napiorkowski, C<< <jjnapiork@cpan.org> >>
266
267=head1 COPYRIGHT & LICENSE
268
269This program is free software; you can redistribute it and/or modify
270it under the same terms as Perl itself.
271
272=cut
273
d8547fef 274__PACKAGE__->meta->make_immutable;