MooseX::Meta::TypeConstraint::Structured::Positional - Structured Type Constraints
-=head1 DESCRIPTION
+=head1 SYNOPSIS
-Structured type constraints let you assign an internal pattern of type
-constraints to a 'container' constraint. The goal is to make it easier to
-declare constraints like "ArrayRef[Int, Int, Str]" where the constraint is an
-ArrayRef of three elements and the internal constraint on the three is Int, Int
-and Str.
+The follow is example usage:
-To accomplish this, we add an attribute to the base L<Moose::Meta::TypeConstraint>
-to hold a L</signature>, which is a reference to a pattern of type constraints.
-We then override L</constraint> to check our incoming value to the attribute
-against this signature pattern.
+ use Moose::Util::TypeConstraints;
+ use MooseX::Meta::TypeConstraint::Structured::Positional;
+
+ my @required = ('Str', 'Int');
+ my @optional = ('Object');
+
+ my $tc = MooseX::Meta::TypeConstraint::Structured::Positional->new(
+ name => 'Dict',
+ parent => find_type_constraint('ArrayRef'),
+ signature => [map {
+ find_type_constraint($_);
+ } @required],
+ optional_signature => [map {
+ find_type_constraint($_);
+ } @optional],
+ );
+
+=head1 DESCRIPTION
Positionally structured Constraints expect the internal constraints to be in
-'positioned' or ArrayRef style order.
+'positioned' or ArrayRef style order. This allows you to add type constraints
+to the internal values of the Arrayref.
=head1 ATTRIBUTES
return sub {
my @args = $self->_normalize_args(shift);
my @signature = @{$self->signature};
- my @optional_signature = @{$self->optional_signature}
- if $self->has_optional_signature;
+ my @optional_signature = @{$self->optional_signature}
+ if $self->has_optional_signature;
## First make sure all the required type constraints match
while( my $type_constraint = shift @signature) {
## Now test the option type constraints.
while( my $arg = shift @args) {
- my $optional_type_constraint = shift @optional_signature;
- if(my $error = $optional_type_constraint->validate($arg)) {
- confess $error;
- }
+ if(my $optional_type_constraint = shift @optional_signature) {
+ if(my $error = $optional_type_constraint->validate($arg)) {
+ confess $error;
+ }
+ } else {
+ confess "Too Many arguments for the available type constraints";
+ }
}
## If we got this far we passed!