package MooseX::Meta::TypeConstraint::Structured;
-use 5.8.8; ## Minimum tested Perl Version
use Moose;
-use Moose::Util::TypeConstraints;
-
+use Moose::Util::TypeConstraints ();
extends 'Moose::Meta::TypeConstraint';
-our $AUTHORITY = 'cpan:JJNAPIORK';
-
=head1 NAME
-MooseX::Meta::TypeConstraint::Structured - Structured Type Constraints
-
-=head1 VERSION
-
-0.01
-
-=cut
-
-our $VERSION = '0.01';
+MooseX::Meta::TypeConstraint::Structured - Structured type constraints.
=head1 DESCRIPTION
-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.
+A structure is a set of L<Moose::Meta::TypeConstraint> that are 'aggregated' in
+such a way as that they are all applied to an incoming list of arguments. The
+idea here is that a Type Constraint could be something like, "An Int followed by
+an Int and then a Str" and that this could be done so with a declaration like:
+
+ Tuple[Int,Int,Str]; ## Example syntax
+
+So a structure is a list of Type constraints (the "Int,Int,Str" in the above
+example) which are intended to function together.
=head1 ATTRIBUTES
This class defines the following attributes.
-=head2 parent
+=head2 type_constraints
-additional details on the inherited parent attribute
-
-=head2 signature
-
-This is a signature of internal contraints for the contents of the outer
-contraint container.
+A list of L<Moose::Meta::TypeConstraint> objects.
=cut
-has 'signature' => (
+has 'type_constraints' => (
is=>'ro',
isa=>'Ref',
- required=>1,
+ predicate=>'has_type_constraints',
);
+=head2 constraint_generator
+
+A subref or closure that contains the way we validate incoming values against
+a set of type constraints.
+
+=cut
+
+has 'constraint_generator' => (is=>'ro', isa=>'CodeRef');
+
=head1 METHODS
This class defines the following methods.
-=head2 _normalize_args
+=head2 generate_constraint_for ($type_constraints)
-Get arguments into a known state or die trying
+Given some type constraints, use them to generate validation rules for an ref
+of values (to be passed at check time)
=cut
-sub _normalize_args {
- my ($self, $args) = @_;
- if(defined $args && ref $args eq 'ARRAY') {
- return @{$args};
- } else {
- confess 'Arguments not ArrayRef as expected.';
- }
+sub generate_constraint_for {
+ my ($self, $type_constraints) = @_;
+ return sub {
+ my $constraint_generator = $self->constraint_generator;
+ return $constraint_generator->($type_constraints, @_);
+ };
}
-
-=head2 constraint
-The constraint is basically validating the L</signature> against the incoming
+=head2 parameterize (@type_constraints)
+
+Given a ref of type constraints, create a structured type.
=cut
-sub constraint {
- my $self = shift;
- return sub {
- my @args = $self->_normalize_args(shift);
- foreach my $idx (0..$#args) {
- if(my $error = $self->signature->[$idx]->validate($args[$idx])) {
- confess $error;
- }
- } 1;
- };
+sub parameterize {
+ my ($self, @type_constraints) = @_;
+ my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
+
+ return __PACKAGE__->new(
+ name => $name,
+ parent => $self,
+ type_constraints => \@type_constraints,
+ constraint_generator => $self->constraint_generator || sub {
+ my $tc = shift @_;
+ my $merged_tc = [@$tc, @{$self->parent->type_constraints}];
+ $self->constraint->($merged_tc, @_);
+ },
+ );
}
+=head2 compile_type_constraint
+
+hook into compile_type_constraint so we can set the correct validation rules.
+
+=cut
+
+around 'compile_type_constraint' => sub {
+ my ($compile_type_constraint, $self, @args) = @_;
+
+ if($self->has_type_constraints) {
+ my $type_constraints = $self->type_constraints;
+ my $constraint = $self->generate_constraint_for($type_constraints);
+ $self->_set_constraint($constraint);
+ }
+
+ return $self->$compile_type_constraint(@args);
+};
+
+=head1 SEE ALSO
+
+The following modules or resources may be of interest.
+
+L<Moose>, L<Moose::Meta::TypeConstraint>
+
=head1 AUTHOR
-John James Napiorkowski <jjnapiork@cpan.org>
+John Napiorkowski, C<< <jjnapiork@cpan.org> >>
-=head1 LICENSE
+=head1 COPYRIGHT & LICENSE
-You may distribute this code under the same terms as Perl itself.
+This program is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
=cut
-no Moose; 1;
+1;
\ No newline at end of file