rollback some stuff to reset my brain a bit
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Meta / TypeConstraint / Structured / Named.pm
index 8c0b1d2..ee4b195 100644 (file)
@@ -10,21 +10,34 @@ with 'MooseX::Meta::TypeConstraint::Role::Structured';
 
 MooseX::Meta::TypeConstraint::Structured::Named - Structured Type Constraints
 
-=head1 DESCRIPTION
+=head1 SYNOPSIS
 
-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.
+The follow is example usage:
 
-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.
+    use Moose::Util::TypeConstraints;
+    use MooseX::Meta::TypeConstraint::Structured::Named;
+    
+    my %required = (key1='Str', key2=>'Int');
+    my %optional = (key3=>'Object');
+    
+    my $tc = MooseX::Meta::TypeConstraint::Structured::Named->new(
+        name => 'Dict',
+        parent => find_type_constraint('HashRef'),
+        package_defined_in => __PACKAGE__,
+        signature => {map {
+            $_ => find_type_constraint($required{$_});
+        } keys %required},
+        optional_signature => {map {
+            $_ => find_type_constraint($optional{$_});
+        } keys %optional},
+    );
+
+=head1 DESCRIPTION
 
 Named structured Constraints expect the internal constraints to be in keys or
-fields similar to what we expect in a HashRef.
+fields similar to what we expect in a HashRef.  Basically, this allows you to
+easily add type constraint checks against values in the wrapping HashRef
+identified by the key name.
 
 =head1 ATTRIBUTES
 
@@ -82,22 +95,20 @@ sub constraint {
     my $self = shift;
     return sub {
         my %args = $self->_normalize_args(shift);
-        my @signature = keys %{$self->signature};
-        my @ptional_signature = keys %{$self->optional_signature}
-         if $self->has_optional_signature;
         
         ## First make sure all the required type constraints match        
-        while( my $type_constraint_key = shift @signature) {
-            my $type_constraint = $self->signature->{$type_constraint_key};
-            if(my $error = $type_constraint->validate($args{$type_constraint_key})) {
+        foreach my $sig_key (keys %{$self->signature}) {
+            my $type_constraint = $self->signature->{$sig_key};
+            if(my $error = $type_constraint->validate($args{$sig_key})) {
                 confess $error;
+            } else {
+                delete $args{$sig_key};
             }
-            delete $args{$type_constraint_key};
         }
         
         ## Now test the option type constraints.
-        while( my $arg_key = keys %args) {
-            my $optional_type_constraint = $self->signature->{$arg_key};
+        foreach my $arg_key (keys %args) {
+            my $optional_type_constraint = $self->optional_signature->{$arg_key};
             if(my $error = $optional_type_constraint->validate($args{$arg_key})) {
                 confess $error;
             }