1 package MooseX::Meta::TypeConstraint::Structured;
4 use Moose::Util::TypeConstraints ();
5 extends 'Moose::Meta::TypeConstraint';
9 MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
13 A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
14 such a way as that they are all applied to an incoming list of arguments. The
15 idea here is that a Type Constraint could be something like, "An Int followed by
16 an Int and then a Str" and that this could be done so with a declaration like:
18 Tuple[Int,Int,Str]; ## Example syntax
20 So a structure is a list of Type constraints (the "Int,Int,Str" in the above
21 example) which are intended to function together.
25 This class defines the following attributes.
27 =head2 type_constraints
29 A list of L<Moose::Meta::TypeConstraint> objects.
33 has 'type_constraints' => (
36 predicate=>'has_type_constraints',
39 =head2 constraint_generator
41 A subref or closure that contains the way we validate incoming values against
42 a set of type constraints.
46 has 'constraint_generator' => (is=>'ro', isa=>'CodeRef');
50 This class defines the following methods.
52 =head2 generate_constraint_for ($type_constraints)
54 Given some type constraints, use them to generate validation rules for an ref
55 of values (to be passed at check time)
59 sub generate_constraint_for {
60 my ($self, $type_constraints) = @_;
62 my $constraint_generator = $self->constraint_generator;
63 return $constraint_generator->($type_constraints, @_);
67 =head2 parameterize (@type_constraints)
69 Given a ref of type constraints, create a structured type.
74 my ($self, @type_constraints) = @_;
75 my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
76 return __PACKAGE__->new(
79 type_constraints => \@type_constraints,
80 constraint_generator => $self->constraint_generator,
84 =head2 compile_type_constraint
86 hook into compile_type_constraint so we can set the correct validation rules.
90 around 'compile_type_constraint' => sub {
91 my ($compile_type_constraint, $self, @args) = @_;
93 if($self->has_type_constraints) {
94 my $type_constraints = $self->type_constraints;
95 my $constraint = $self->generate_constraint_for($type_constraints);
96 $self->_set_constraint($constraint);
99 return $self->$compile_type_constraint(@args);
104 The following modules or resources may be of interest.
106 L<Moose>, L<Moose::Meta::TypeConstraint>
110 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
112 =head1 COPYRIGHT & LICENSE
114 This program is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.