-package MooseX::Meta::TypeConstraint::Structured;
+package ## Hide from PAUSE
+ MooseX::Meta::TypeConstraint::Structured;
-use 5.8.8; ## Minimum tested Perl Version
use Moose;
-use Moose::Util::TypeConstraints;
-
+use Devel::PartialDump;
+use Moose::Util::TypeConstraints ();
+use MooseX::Meta::TypeCoercion::Structured;
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:
-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.
+ 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 SUBTYPES
+=head1 ATTRIBUTES
-The following subtypes and coercions are defined in this class.
+This class defines the following attributes.
-=head2 MooseX::Meta::TypeConstraint::Structured::Signature
+=head2 type_constraints
-This is a type constraint to normalize the incoming L</signature>. We want
-everything as a HashRef in the end.
+A list of L<Moose::Meta::TypeConstraint> objects.
=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;
- };
-
-=head1 ATTRIBUTES
-
-This class defines the following attributes.
+has 'type_constraints' => (
+ is=>'ro',
+ isa=>'Ref',
+ predicate=>'has_type_constraints',
+);
-=head2 signature
+=head2 constraint_generator
-This is a signature of internal contraints for the contents of the outer
-contraint container.
+A subref or closure that contains the way we validate incoming values against
+a set of type constraints.
=cut
-has 'signature' => (
+has 'constraint_generator' => (
is=>'ro',
- isa=>'MooseX::Meta::TypeConstraint::Structured::Signature',
- coerce=>1,
- required=>1,
+ isa=>'CodeRef',
+ predicate=>'has_constraint_generator',
);
=head1 METHODS
This class defines the following methods.
-=head2 _normalize_args
+=head2 new
-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.
+Initialization stuff.
=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;
+around 'new' => sub {
+ my ($new, $class, @args) = @_;
+ my $self = $class->$new(@args);
+ $self->coercion(MooseX::Meta::TypeCoercion::Structured->new(
+ type_constraint => $self,
+ ));
+ return $self;
+};
+
+=head2 validate
+
+Messing with validate so that we can support niced error messages.
+=cut
+
+override 'validate' => sub {
+ my ($self, @args) = @_;
+ my $message = bless {message=>undef}, 'MooseX::Types::Structured::Message';
+
+ if ($self->_compiled_type_constraint->(@args, $message)) {
+ ## Everything is good, no error message to return
+ return undef;
+ } else {
+ ## Whoops, need to figure out the right error message
+ my $args = Devel::PartialDump::dump(@args);
+ if(my $message = $message->{message}) {
+ return $self->get_message("$args, Internal Validation Error is: $message");
} else {
- confess 'Signature must be a reference';
+ return $self->get_message($args);
}
- } else {
- confess 'Signature cannot be empty';
}
-}
-
-=head2 constraint
+};
+
+=head2 generate_constraint_for ($type_constraints)
-The constraint is basically validating the L</signature> against the incoming
+Given some type constraints, use them to generate validation rules for an ref
+of values (to be passed at check time)
=cut
-sub constraint {
- my $self = shift;
+sub generate_constraint_for {
+ my ($self, $type_constraints) = @_;
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;
+ my $arg = shift @_;
+ my $constraint_generator = $self->constraint_generator;
+ my $result = $constraint_generator->($type_constraints, $arg, $_[0]);
+ return $result;
};
}
-=head2 equals
+=head2 parameterize (@type_constraints)
-modifier to make sure equals descends into the L</signature>
+Given a ref of type constraints, create a structured type.
=cut
-around 'equals' => sub {
- my ($equals, $self, $compared_type_constraint) = @_;
-
- ## Make sure we are comparing typeconstraints of the same base class
- return unless $compared_type_constraint->isa(__PACKAGE__);
+sub parameterize {
- ## Make sure the base equals is also good
- return unless $self->$equals($compared_type_constraint);
+ my ($self, @type_constraints) = @_;
+ my $class = ref $self;
+ my $name = $self->name .'['. join(',', map {"$_"} @type_constraints) .']';
+ my $constraint_generator = $self->__infer_constraint_generator;
+
+ return $class->new(
+ name => $name,
+ parent => $self,
+ type_constraints => \@type_constraints,
+ constraint_generator => $constraint_generator,
+ );
+}
+
+=head2 __infer_constraint_generator
+
+This returns a CODEREF which generates a suitable constraint generator. Not
+user servicable, you'll never call this directly.
+
+=cut
+
+sub __infer_constraint_generator {
+ my ($self) = @_;
+ if($self->has_constraint_generator) {
+ return $self->constraint_generator;
+ } else {
+ return sub {
+ ## I'm not sure about this stuff but everything seems to work
+ 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) = @_;
- ## Make sure the signatures match
- return unless $self->signature_equals($compared_type_constraint);
-
- ## If we get this far, the two are equal
- return 1;
+ 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);
};
-=head2 signature_equals
+=head2 create_child_type
+
+modifier to make sure we get the constraint_generator
+
+=cut
+
+around 'create_child_type' => sub {
+ my ($create_child_type, $self, %opts) = @_;
+ return $self->$create_child_type(
+ %opts,
+ constraint_generator => $self->__infer_constraint_generator,
+ );
+};
+
+=head2 is_a_type_of
+
+=head2 is_subtype_of
+
+=head2 equals
+
+Override the base class behavior.
+
+=cut
+
+sub equals {
+ my ( $self, $type_or_name ) = @_;
+ my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
+
+ return unless $other->isa(__PACKAGE__);
+
+ return (
+ $self->type_constraints_equals($other)
+ and
+ $self->parent->equals( $other->parent )
+ );
+}
-Check that the signature equals another signature.
+=head2 type_constraints_equals
+
+Checks to see if the internal type contraints are equal.
=cut
-sub signature_equals {
- my ($self, $compared_type_constraint) = @_;
+sub type_constraints_equals {
+ my ($self, $other) = @_;
+ my @self_type_constraints = @{$self->type_constraints||[]};
+ my @other_type_constraints = @{$other->type_constraints||[]};
- foreach my $idx (keys %{$self->signature}) {
- my $this = $self->signature->{$idx};
- my $that = $compared_type_constraint->signature->{$idx};
- return unless $this->equals($that);
+ ## Incoming ay be either arrayref or hashref, need top compare both
+ while(@self_type_constraints) {
+ my $self_type_constraint = shift @self_type_constraints;
+ my $other_type_constraint = shift @other_type_constraints
+ || return; ## $other needs the same number of children.
+
+ if( ref $self_type_constraint) {
+ $self_type_constraint->equals($other_type_constraint)
+ || return; ## type constraints obviously need top be equal
+ } else {
+ $self_type_constraint eq $other_type_constraint
+ || return; ## strings should be equal
+ }
+
}
-
- return 1;
+
+ return 1; ##If we get this far, everything is good.
}
+=head2 get_message
+
+Give you a better peek into what's causing the error. For now we stringify the
+incoming deep value with L<Devel::PartialDump> and pass that on to either your
+custom error message or the default one. In the future we'll try to provide a
+more complete stack trace of the actual offending elements
+
+=cut
+
+around 'get_message' => sub {
+ my ($get_message, $self, $value) = @_;
+ $value = Devel::PartialDump::dump($value)
+ if ref $value;
+ return $self->$get_message($value);
+};
+
+=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;
+__PACKAGE__->meta->make_immutable;
\ No newline at end of file