use Moose::Role;
use Moose::Util::TypeConstraints;
+requires qw(_normalize_args signature_equals);
=head1 NAME
MooseX::Meta::TypeConstraint::Role::Structured - Structured Type Constraints
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
-
=head1 DESCRIPTION
+This Role defines the interface and basic behavior of Structured Type Constraints.
+
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
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.
+against this signature pattern. Additionally we allow L</optional_signature> to
+hold any optional type constraints. The overall goal is to support something
+like:
+
+ has 'attr' => (isa=>'Tuple[Int, Str, Optional[Int, Int]]');
-=head1 SUBTYPES
+These classes define how the underlying support for this works.
-The following subtypes and coercions are defined in this class.
+=head1 TYPES
-=head2 MooseX::Meta::TypeConstraint::Structured::Signature
+The following types are defined in this class.
-This is a type constraint to normalize the incoming L</signature>. We want
-everything as a HashRef in the end.
+=head2 Moose::Meta::TypeConstraint
+
+Used to make sure we can properly validate incoming signatures.
=cut
-subtype 'MooseX::Meta::TypeConstraint::Structured::Signature',
- as 'HashRef[Object]',
- where {
- my %signature = %$_;
- foreach my $key (keys %signature) {
- $signature{$key}->isa('Moose::Meta::TypeConstraint');
- } 1;
- };
-
-coerce 'MooseX::Meta::TypeConstraint::Structured::Signature',
- from 'ArrayRef[Object]',
- via {
- my @signature = @$_;
- my %hashed_signature = map { $_ => $signature[$_] } 0..$#signature;
- \%hashed_signature;
- };
+class_type 'Moose::Meta::TypeConstraint';
=head1 ATTRIBUTES
has 'signature' => (
is=>'ro',
- isa=>'MooseX::Meta::TypeConstraint::Structured::Signature',
- coerce=>1,
+ isa=>'Ref',
required=>1,
);
-=head1 METHODS
-
-This class defines the following methods.
+=head2 optional_signature
-=head2 _normalize_args
-
-Get arguments into a known state or die trying. Ideally we try to make this
-into a HashRef so we can match it up with the L</signature> HashRef.
+This is a signature of internal contraints for the contents of the outer
+contraint container. These are optional constraints.
=cut
-sub _normalize_args {
- my ($self, $args) = @_;
- if(defined $args) {
- if(ref $args eq 'ARRAY') {
- return map { $_ => $args->[$_] } (0..$#$args);
- } elsif (ref $args eq 'HASH') {
- return %$args;
- } else {
- confess 'Signature must be a reference';
- }
- } else {
- confess 'Signature cannot be empty';
- }
-}
-
-=head2 constraint
-
-The constraint is basically validating the L</signature> against the incoming
+has 'optional_signature' => (
+ is=>'ro',
+ isa=>'Ref',
+ predicate=>'has_optional_signature',
+);
-=cut
+=head1 METHODS
-sub constraint {
- my $self = shift;
- return sub {
- my %args = $self->_normalize_args(shift);
- foreach my $idx (keys %{$self->signature}) {
- my $type_constraint = $self->signature->{$idx};
- if(my $error = $type_constraint->validate($args{$idx})) {
- confess $error;
- }
- } 1;
- };
-}
+This class defines the following methods.
=head2 equals
return 1;
};
-=head2 signature_equals
-
-Check that the signature equals another signature.
-
-=cut
-
-sub signature_equals {
- my ($self, $compared_type_constraint) = @_;
-
- foreach my $idx (keys %{$self->signature}) {
- my $this = $self->signature->{$idx};
- my $that = $compared_type_constraint->signature->{$idx};
- return unless $this->equals($that);
- }
-
- return 1;
-}
-
=head1 AUTHOR
John James Napiorkowski <jjnapiork@cpan.org>
=cut
-no Moose; 1;
+1;