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->parent->equals($other->parent)
207 $self->type_constraints_equals($other)
212 my ( $self, $type_or_name ) = @_;
213 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
215 if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) {
216 if ( $self->parent->is_a_type_of($other->parent) ) {
217 return $self->_type_constraints_op_all($other, "is_a_type_of");
218 } elsif ( $self->parent->is_a_type_of($other) ) {
225 return $self->SUPER::is_a_type_of($other);
230 my ( $self, $type_or_name ) = @_;
232 my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
234 if ( $other->isa(__PACKAGE__) ) {
235 if ( $other->type_constraints and $self->type_constraints ) {
236 if ( $self->parent->is_a_type_of($other->parent) ) {
238 $self->_type_constraints_op_all($other, "is_a_type_of")
240 $self->_type_constraints_op_any($other, "is_subtype_of")
242 } elsif ( $self->parent->is_a_type_of($other) ) {
249 if ( $self->type_constraints ) {
250 if ( $self->SUPER::is_subtype_of($other) ) {
256 return $self->parent->is_subtype_of($other->parent);
260 return $self->SUPER::is_subtype_of($other);
264 =head2 type_constraints_equals
266 Checks to see if the internal type contraints are equal.
270 sub type_constraints_equals {
271 my ( $self, $other ) = @_;
272 $self->_type_constraints_op_all($other, "equals");
275 sub _type_constraints_op_all {
276 my ($self, $other, $op) = @_;
278 return unless $other->isa(__PACKAGE__);
280 my @self_type_constraints = @{$self->type_constraints||[]};
281 my @other_type_constraints = @{$other->type_constraints||[]};
283 return unless @self_type_constraints == @other_type_constraints;
285 ## Incoming ay be either arrayref or hashref, need top compare both
286 while(@self_type_constraints) {
287 my $self_type_constraint = shift @self_type_constraints;
288 my $other_type_constraint = shift @other_type_constraints;
290 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
291 for $self_type_constraint, $other_type_constraint;
293 $self_type_constraint->$op($other_type_constraint) or return;
296 return 1; ##If we get this far, everything is good.
299 sub _type_constraints_op_any {
300 my ($self, $other, $op) = @_;
302 return unless $other->isa(__PACKAGE__);
304 my @self_type_constraints = @{$self->type_constraints||[]};
305 my @other_type_constraints = @{$other->type_constraints||[]};
307 return unless @self_type_constraints == @other_type_constraints;
309 ## Incoming ay be either arrayref or hashref, need top compare both
310 while(@self_type_constraints) {
311 my $self_type_constraint = shift @self_type_constraints;
312 my $other_type_constraint = shift @other_type_constraints;
314 $_ = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($_)
315 for $self_type_constraint, $other_type_constraint;
317 return 1 if $self_type_constraint->$op($other_type_constraint);
325 Give you a better peek into what's causing the error. For now we stringify the
326 incoming deep value with L<Devel::PartialDump> and pass that on to either your
327 custom error message or the default one. In the future we'll try to provide a
328 more complete stack trace of the actual offending elements
332 around 'get_message' => sub {
333 my ($get_message, $self, $value) = @_;
334 $value = Devel::PartialDump::dump($value)
336 return $self->$get_message($value);
341 The following modules or resources may be of interest.
343 L<Moose>, L<Moose::Meta::TypeConstraint>
347 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
349 =head1 COPYRIGHT & LICENSE
351 This program is free software; you can redistribute it and/or modify
352 it under the same terms as Perl itself.
356 __PACKAGE__->meta->make_immutable;