X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FMeta%2FTypeConstraint%2FStructured%2FPositional.pm;h=d29ed46b93a7bcf366214d4259ef34961dd02ac8;hb=78f559467710da345f5d08c2fea40da4d75ed8ee;hp=83d16af37c5b8ae50b1ea76f55818e9e274f7bca;hpb=b5f77bd36c5f51e2b54010ca83b032af645f1aeb;p=gitmo%2FMooseX-Types-Structured.git diff --git a/lib/MooseX/Meta/TypeConstraint/Structured/Positional.pm b/lib/MooseX/Meta/TypeConstraint/Structured/Positional.pm index 83d16af..d29ed46 100644 --- a/lib/MooseX/Meta/TypeConstraint/Structured/Positional.pm +++ b/lib/MooseX/Meta/TypeConstraint/Structured/Positional.pm @@ -10,21 +10,32 @@ with 'MooseX::Meta::TypeConstraint::Role::Structured'; 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 -to hold a L, which is a reference to a pattern of type constraints. -We then override L 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 @@ -83,8 +94,8 @@ sub constraint { 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) { @@ -95,10 +106,13 @@ sub constraint { ## 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!