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 This class defines the following attributes.
37 additional details on the inherited parent attribute
41 This is a signature of internal contraints for the contents of the outer
46 subtype 'MooseX::Meta::TypeConstraint::Structured::Signature',
50 foreach my $key (keys %signature) {
51 $signature{$key}->isa('Moose::Meta::TypeConstraint');
55 coerce 'MooseX::Meta::TypeConstraint::Structured::Signature',
56 from 'ArrayRef[Object]',
59 my %hashed_signature = map { $_ => $signature[$_] } 0..$#signature;
65 isa=>'MooseX::Meta::TypeConstraint::Structured::Signature',
72 This class defines the following methods.
74 =head2 _normalize_args
76 Get arguments into a known state or die trying. Ideally we try to make this
77 into a HashRef so we can match it up with the L</signature> HashRef.
82 my ($self, $args) = @_;
84 if(ref $args eq 'ARRAY') {
85 return map { $_ => $args->[$_] } (0..$#$args);
86 } elsif (ref $args eq 'HASH') {
89 confess 'Signature must be a reference';
92 confess 'Signature cannot be empty';
98 The constraint is basically validating the L</signature> against the incoming
105 my %args = $self->_normalize_args(shift);
106 foreach my $idx (keys %{$self->signature}) {
107 my $type_constraint = $self->signature->{$idx};
108 if(my $error = $type_constraint->validate($args{$idx})) {
117 modifier to make sure equals descends into the L</signature>
121 around 'equals' => sub {
122 my ($equals, $self, $compared_type_constraint) = @_;
124 ## Make sure we are comparing typeconstraints of the same base class
125 return unless $compared_type_constraint->isa(__PACKAGE__);
127 ## Make sure the base equals is also good
128 return unless $self->$equals($compared_type_constraint);
130 ## Make sure the signatures match
131 return unless $self->signature_equals($compared_type_constraint);
133 ## If we get this far, the two are equal
137 =head2 signature_equals
139 Check that the signature equals another signature.
143 sub signature_equals {
144 my ($self, $compared_type_constraint) = @_;
146 foreach my $idx (keys %{$self->signature}) {
147 my $this = $self->signature->{$idx};
148 my $that = $compared_type_constraint->signature->{$idx};
149 return unless $this->equals($that);
157 John James Napiorkowski <jjnapiork@cpan.org>
161 You may distribute this code under the same terms as Perl itself.