1 package MooseX::Meta::TypeConstraint::Structured;
3 use 5.8.8; ## Minimum tested Perl Version
5 use Moose::Util::TypeConstraints;
7 extends 'Moose::Meta::TypeConstraint';
9 our $AUTHORITY = 'cpan:JJNAPIORK';
13 MooseX::Meta::TypeConstraint::Structured - Structured Type Constraints
21 our $VERSION = '0.01';
25 Structured type constraints let you assign an internal pattern of type
26 constraints to a 'container' constraint. The goal is to make it easier to
27 declare constraints like "ArrayRef[Int, Int, Str]" where the constraint is an
28 ArrayRef of three elements and the internal constraint on the three is Int, Int
33 The following subtypes and coercions are defined in this class.
35 =head2 MooseX::Meta::TypeConstraint::Structured::Signature
37 This is a type constraint to normalize the incoming L</signature>.
41 subtype 'MooseX::Meta::TypeConstraint::Structured::Signature',
45 foreach my $key (keys %signature) {
46 $signature{$key}->isa('Moose::Meta::TypeConstraint');
50 coerce 'MooseX::Meta::TypeConstraint::Structured::Signature',
51 from 'ArrayRef[Object]',
54 my %hashed_signature = map { $_ => $signature[$_] } 0..$#signature;
60 This class defines the following attributes.
64 This is a signature of internal contraints for the contents of the outer
71 isa=>'MooseX::Meta::TypeConstraint::Structured::Signature',
78 This class defines the following methods.
80 =head2 _normalize_args
82 Get arguments into a known state or die trying. Ideally we try to make this
83 into a HashRef so we can match it up with the L</signature> HashRef.
88 my ($self, $args) = @_;
90 if(ref $args eq 'ARRAY') {
91 return map { $_ => $args->[$_] } (0..$#$args);
92 } elsif (ref $args eq 'HASH') {
95 confess 'Signature must be a reference';
98 confess 'Signature cannot be empty';
104 The constraint is basically validating the L</signature> against the incoming
111 my %args = $self->_normalize_args(shift);
112 foreach my $idx (keys %{$self->signature}) {
113 my $type_constraint = $self->signature->{$idx};
114 if(my $error = $type_constraint->validate($args{$idx})) {
123 modifier to make sure equals descends into the L</signature>
127 around 'equals' => sub {
128 my ($equals, $self, $compared_type_constraint) = @_;
130 ## Make sure we are comparing typeconstraints of the same base class
131 return unless $compared_type_constraint->isa(__PACKAGE__);
133 ## Make sure the base equals is also good
134 return unless $self->$equals($compared_type_constraint);
136 ## Make sure the signatures match
137 return unless $self->signature_equals($compared_type_constraint);
139 ## If we get this far, the two are equal
143 =head2 signature_equals
145 Check that the signature equals another signature.
149 sub signature_equals {
150 my ($self, $compared_type_constraint) = @_;
152 foreach my $idx (keys %{$self->signature}) {
153 my $this = $self->signature->{$idx};
154 my $that = $compared_type_constraint->signature->{$idx};
155 return unless $this->equals($that);
163 John James Napiorkowski <jjnapiork@cpan.org>
167 You may distribute this code under the same terms as Perl itself.