1 package ## Hide from PAUSE
2 MooseX::Meta::TypeConstraint::Structured;
5 use Devel::PartialDump;
6 use Moose::Util::TypeConstraints ();
7 use MooseX::Meta::TypeCoercion::Structured;
8 extends 'Moose::Meta::TypeConstraint';
12 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
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:
21 Tuple[Int,Int,Str]; ## Example syntax
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.
28 This class defines the following attributes.
30 =head2 type_constraints
32 A list of L<Moose::Meta::TypeConstraint> objects.
36 has 'type_constraints' => (
39 predicate=>'has_type_constraints',
42 =head2 constraint_generator
44 A subref or closure that contains the way we validate incoming values against
45 a set of type constraints.
49 has 'constraint_generator' => (
52 predicate=>'has_constraint_generator',
57 This class defines the following methods.
66 my ($new, $class, @args) = @_;
67 my $self = $class->$new(@args);
68 $self->coercion(MooseX::Meta::TypeCoercion::Structured->new(
69 type_constraint => $self,
74 =head2 generate_constraint_for ($type_constraints)
76 Given some type constraints, use them to generate validation rules for an ref
77 of values (to be passed at check time)
81 sub generate_constraint_for {
82 my ($self, $type_constraints) = @_;
85 my $constraint_generator = $self->constraint_generator;
86 return $constraint_generator->($type_constraints, @args);
90 =head2 parameterize (@type_constraints)
92 Given a ref of type constraints, create a structured type.
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;
106 type_constraints => \@type_constraints,
107 constraint_generator => $constraint_generator,
111 =head2 __infer_constraint_generator
113 This returns a CODEREF which generates a suitable constraint generator. Not
114 user servicable, you'll never call this directly.
118 sub __infer_constraint_generator {
120 if($self->has_constraint_generator) {
121 return $self->constraint_generator;
124 ## I'm not sure about this stuff but everything seems to work
126 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
127 $self->constraint->($merged_tc, @_);
132 =head2 compile_type_constraint
134 hook into compile_type_constraint so we can set the correct validation rules.
138 around 'compile_type_constraint' => sub {
139 my ($compile_type_constraint, $self, @args) = @_;
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);
147 return $self->$compile_type_constraint(@args);
150 =head2 create_child_type
152 modifier to make sure we get the constraint_generator
156 around 'create_child_type' => sub {
157 my ($create_child_type, $self, %opts) = @_;
158 return $self->$create_child_type(
160 constraint_generator => $self->__infer_constraint_generator,
170 Override the base class behavior.
175 my ( $self, $type_or_name ) = @_;
176 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
178 return unless $other->isa(__PACKAGE__);
181 $self->type_constraints_equals($other)
183 $self->parent->equals( $other->parent )
187 =head2 type_constraints_equals
189 Checks to see if the internal type contraints are equal.
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||[]};
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.
204 if( ref $self_type_constraint) {
205 $self_type_constraint->equals($other_type_constraint)
206 || return; ## type constraints obviously need top be equal
208 $self_type_constraint eq $other_type_constraint
209 || return; ## strings should be equal
214 return 1; ##If we get this far, everything is good.
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
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);
235 The following modules or resources may be of interest.
237 L<Moose>, L<Moose::Meta::TypeConstraint>
241 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
243 =head1 COPYRIGHT & LICENSE
245 This program is free software; you can redistribute it and/or modify
246 it under the same terms as Perl itself.
250 __PACKAGE__->meta->make_immutable;