34ab8e198f07dce0f84fb21b863f2dc41d9f1335
[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 generate_constraint_for ($type_constraints)
75
76 Given some type constraints, use them to generate validation rules for an ref
77 of values (to be passed at check time)
78
79 =cut
80
81 sub generate_constraint_for {
82     my ($self, $type_constraints) = @_;
83     return sub {
84         my (@args) = @_;
85         my $constraint_generator = $self->constraint_generator;
86         return $constraint_generator->($type_constraints, @args);
87     };
88 }
89
90 =head2 parameterize (@type_constraints)
91
92 Given a ref of type constraints, create a structured type.
93
94 =cut
95
96 sub parameterize {
97     
98     my ($self, @type_constraints) = @_;
99     my $class = ref $self;
100     my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
101     my $constraint_generator = $self->__infer_constraint_generator;
102
103     return $class->new(
104         name => $name,
105         parent => $self,
106         type_constraints => \@type_constraints,
107         constraint_generator => $constraint_generator,
108     );
109 }
110
111 =head2 __infer_constraint_generator
112
113 This returns a CODEREF which generates a suitable constraint generator.  Not
114 user servicable, you'll never call this directly.
115
116 =cut
117
118 sub __infer_constraint_generator {
119     my ($self) = @_;
120     if($self->has_constraint_generator) {
121         return $self->constraint_generator;
122     } else {
123         return sub {
124             ## I'm not sure about this stuff but everything seems to work
125             my $tc = shift @_;
126             my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
127             $self->constraint->($merged_tc, @_);            
128         };
129     }    
130 }
131
132 =head2 compile_type_constraint
133
134 hook into compile_type_constraint so we can set the correct validation rules.
135
136 =cut
137
138 around 'compile_type_constraint' => sub {
139     my ($compile_type_constraint, $self, @args) = @_;
140     
141     if($self->has_type_constraints) {
142         my $type_constraints = $self->type_constraints;
143         my $constraint = $self->generate_constraint_for($type_constraints);
144         $self->_set_constraint($constraint);        
145     }
146
147     return $self->$compile_type_constraint(@args);
148 };
149
150 =head2 create_child_type
151
152 modifier to make sure we get the constraint_generator
153
154 =cut
155
156 around 'create_child_type' => sub {
157     my ($create_child_type, $self, %opts) = @_;
158     return $self->$create_child_type(
159         %opts,
160         constraint_generator => $self->__infer_constraint_generator,
161     );
162 };
163
164 =head2 is_a_type_of
165
166 =head2 is_subtype_of
167
168 =head2 equals
169
170 Override the base class behavior.
171
172 =cut
173
174 sub equals {
175     my ( $self, $type_or_name ) = @_;
176     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
177
178     return unless $other->isa(__PACKAGE__);
179     
180     return (
181         $self->type_constraints_equals($other)
182             and
183         $self->parent->equals( $other->parent )
184     );
185 }
186
187 =head2 type_constraints_equals
188
189 Checks to see if the internal type contraints are equal.
190
191 =cut
192
193 sub type_constraints_equals {
194     my ($self, $other) = @_;
195     my @self_type_constraints = @{$self->type_constraints||[]};
196     my @other_type_constraints = @{$other->type_constraints||[]};
197     
198     ## Incoming ay be either arrayref or hashref, need top compare both
199     while(@self_type_constraints) {
200         my $self_type_constraint = shift @self_type_constraints;
201         my $other_type_constraint = shift @other_type_constraints
202          || return; ## $other needs the same number of children.
203         
204         if( ref $self_type_constraint) {
205             $self_type_constraint->equals($other_type_constraint)
206              || return; ## type constraints obviously need top be equal
207         } else {
208             $self_type_constraint eq $other_type_constraint
209              || return; ## strings should be equal
210         }
211
212     }
213     
214     return 1; ##If we get this far, everything is good.
215 }
216
217 =head2 get_message
218
219 Give you a better peek into what's causing the error.  For now we stringify the
220 incoming deep value with L<Devel::PartialDump> and pass that on to either your
221 custom error message or the default one.  In the future we'll try to provide a
222 more complete stack trace of the actual offending elements
223
224 =cut
225
226 around 'get_message' => sub {
227     my ($get_message, $self, $value) = @_;
228     my $new_value = Devel::PartialDump::dump($value);
229     return $self->$get_message($new_value);
230     
231 };
232
233 =head1 SEE ALSO
234
235 The following modules or resources may be of interest.
236
237 L<Moose>, L<Moose::Meta::TypeConstraint>
238
239 =head1 AUTHOR
240
241 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
242
243 =head1 COPYRIGHT & LICENSE
244
245 This program is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.
247
248 =cut
249
250 __PACKAGE__->meta->make_immutable;