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,
76 Messing with validate so that we can support niced error messages.
79 override 'validate' => sub {
80 my ($self, @args) = @_;
81 my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
83 if ($self->_compiled_type_constraint->(@args, $message)) {
84 ## Everything is good, no error message to return
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");
92 return $self->get_message($args);
97 =head2 generate_constraint_for ($type_constraints)
99 Given some type constraints, use them to generate validation rules for an ref
100 of values (to be passed at check time)
104 sub generate_constraint_for {
105 my ($self, $type_constraints) = @_;
108 my $constraint_generator = $self->constraint_generator;
109 my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
114 =head2 parameterize (@type_constraints)
116 Given a ref of type constraints, create a structured type.
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;
130 type_constraints => \@type_constraints,
131 constraint_generator => $constraint_generator,
135 =head2 __infer_constraint_generator
137 This returns a CODEREF which generates a suitable constraint generator. Not
138 user servicable, you'll never call this directly.
142 sub __infer_constraint_generator {
144 if($self->has_constraint_generator) {
145 return $self->constraint_generator;
148 ## I'm not sure about this stuff but everything seems to work
150 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
151 $self->constraint->($merged_tc, @_);
156 =head2 compile_type_constraint
158 hook into compile_type_constraint so we can set the correct validation rules.
162 around 'compile_type_constraint' => sub {
163 my ($compile_type_constraint, $self, @args) = @_;
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);
171 return $self->$compile_type_constraint(@args);
174 =head2 create_child_type
176 modifier to make sure we get the constraint_generator
180 around 'create_child_type' => sub {
181 my ($create_child_type, $self, %opts) = @_;
182 return $self->$create_child_type(
184 constraint_generator => $self->__infer_constraint_generator,
194 Override the base class behavior.
199 my ( $self, $type_or_name ) = @_;
200 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
202 return unless $other->isa(__PACKAGE__);
205 $self->type_constraints_equals($other)
207 $self->parent->equals( $other->parent )
211 =head2 type_constraints_equals
213 Checks to see if the internal type contraints are equal.
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||[]};
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.
228 if( ref $self_type_constraint) {
229 $self_type_constraint->equals($other_type_constraint)
230 || return; ## type constraints obviously need top be equal
232 $self_type_constraint eq $other_type_constraint
233 || return; ## strings should be equal
238 return 1; ##If we get this far, everything is good.
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
250 around 'get_message' => sub {
251 my ($get_message, $self, $value) = @_;
252 $value = Devel::PartialDump::dump($value)
254 return $self->$get_message($value);
259 The following modules or resources may be of interest.
261 L<Moose>, L<Moose::Meta::TypeConstraint>
265 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
267 =head1 COPYRIGHT & LICENSE
269 This program is free software; you can redistribute it and/or modify
270 it under the same terms as Perl itself.
274 __PACKAGE__->meta->make_immutable;