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) .']';
77 return __PACKAGE__->new(
80 type_constraints => \@type_constraints,
81 constraint_generator => $self->constraint_generator || sub {
83 my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
84 $self->constraint->($merged_tc, @_);
89 =head2 compile_type_constraint
91 hook into compile_type_constraint so we can set the correct validation rules.
95 around 'compile_type_constraint' => sub {
96 my ($compile_type_constraint, $self, @args) = @_;
98 if($self->has_type_constraints) {
99 my $type_constraints = $self->type_constraints;
100 my $constraint = $self->generate_constraint_for($type_constraints);
101 $self->_set_constraint($constraint);
104 return $self->$compile_type_constraint(@args);
107 =head2 create_child_type
109 modifier to make sure we get the constraint_generator
113 around 'create_child_type' => sub {
114 my ($create_child_type, $self, %opts) = @_;
115 return $self->$create_child_type(
117 constraint_generator => $self->constraint_generator,
129 Want to override this to set a more useful error message
133 The following modules or resources may be of interest.
135 L<Moose>, L<Moose::Meta::TypeConstraint>
139 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
141 =head1 COPYRIGHT & LICENSE
143 This program is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.