fixed incorrect alway finding an error with TC->validate
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured.pm
1 package ## Hide from PAUSE
2  MooseX::Meta::TypeConstraint::Structured;
3
4 use Moose;
5 use Devel::PartialDump;
6 use Moose::Util::TypeConstraints ();
7 use MooseX::Meta::TypeCoercion::Structured;
8 extends 'Moose::Meta::TypeConstraint';
9
10 =head1 NAME
11
12 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
13
14 =head1 DESCRIPTION
15
16 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
17 such a way as that they are all applied to an incoming list of arguments.  The
18 idea here is that a Type Constraint could be something like, "An Int followed by
19 an 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     
23 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
24 example) which are intended to function together.
25
26 =head1 ATTRIBUTES
27
28 This class defines the following attributes.
29
30 =head2 type_constraints
31
32 A list of L<Moose::Meta::TypeConstraint> objects.
33
34 =cut
35
36 has 'type_constraints' => (
37     is=>'ro',
38     isa=>'Ref',
39     predicate=>'has_type_constraints',
40 );
41
42 =head2 constraint_generator
43
44 A subref or closure that contains the way we validate incoming values against
45 a set of type constraints.
46
47 =cut
48
49 has 'constraint_generator' => (
50     is=>'ro',
51     isa=>'CodeRef',
52     predicate=>'has_constraint_generator',
53 );
54
55 =head1 METHODS
56
57 This class defines the following methods.
58
59 =head2 new
60
61 Initialization stuff.
62
63 =cut
64
65 around '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
74 =head2 validate
75
76 Messing with validate so that we can support niced error messages.
77 =cut
78
79 override 'validate' => sub {
80     my ($self, @args) = @_;
81     my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
82
83     if ($self->_compiled_type_constraint->(@args, $message)) {
84         ## Everything is good, no error message to return
85         return undef;
86     } else {
87         ## Whoops, need to figure out the right error message
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
97 =head2 generate_constraint_for ($type_constraints)
98
99 Given some type constraints, use them to generate validation rules for an ref
100 of values (to be passed at check time)
101
102 =cut
103
104 sub generate_constraint_for {
105     my ($self, $type_constraints) = @_;
106     return sub {
107         my $arg =  shift @_;
108         my $constraint_generator = $self->constraint_generator;
109         my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
110         return $result;
111     };
112 }
113
114 =head2 parameterize (@type_constraints)
115
116 Given a ref of type constraints, create a structured type.
117
118 =cut
119
120 sub parameterize {
121     
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 =head2 __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 =head2 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 =head2 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 =head2 is_a_type_of
189
190 =head2 is_subtype_of
191
192 =head2 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
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
213 Checks to see if the internal type contraints are equal.
214
215 =cut
216
217 sub 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
241 =head2 get_message
242
243 Give you a better peek into what's causing the error.  For now we stringify the
244 incoming deep value with L<Devel::PartialDump> and pass that on to either your
245 custom error message or the default one.  In the future we'll try to provide a
246 more complete stack trace of the actual offending elements
247
248 =cut
249
250 around 'get_message' => sub {
251     my ($get_message, $self, $value) = @_;
252     $value = Devel::PartialDump::dump($value)
253      if ref $value;
254     return $self->$get_message($value);
255 };
256
257 =head1 SEE ALSO
258
259 The following modules or resources may be of interest.
260
261 L<Moose>, L<Moose::Meta::TypeConstraint>
262
263 =head1 AUTHOR
264
265 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
266
267 =head1 COPYRIGHT & LICENSE
268
269 This program is free software; you can redistribute it and/or modify
270 it under the same terms as Perl itself.
271
272 =cut
273
274 __PACKAGE__->meta->make_immutable;