[Moose-commits] r7460 - MooseX-Params-Validate/trunk/lib/MooseX/Params

autarch at code2.0beta.co.uk autarch at code2.0beta.co.uk
Sun Feb 1 05:44:10 GMT 2009


Author: autarch
Date: 2009-01-31 21:44:09 -0800 (Sat, 31 Jan 2009)
New Revision: 7460

Modified:
   MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
Log:
Lots of refactorings/renamings of bits


Modified: MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
===================================================================
--- MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm	2009-01-31 22:48:38 UTC (rev 7459)
+++ MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm	2009-02-01 05:44:09 UTC (rev 7460)
@@ -6,10 +6,10 @@
 use Carp 'confess';
 use Scalar::Util 'blessed';
 
-use Moose::Util::TypeConstraints ();
+use Moose::Util::TypeConstraints qw( find_type_constraint class_type role_type );
 use Params::Validate             ();
 use Sub::Exporter -setup => {
-    exports => [qw( validated_hash validated_list validate validatep )],
+    exports => [ qw( validated_hash validated_list validate validatep ) ],
     groups  => {
         default    => [qw( validated_hash validated_list )],
         deprecated => [qw( validate validatep )],
@@ -19,36 +19,34 @@
 our $VERSION   = '0.07';
 our $AUTHORITY = 'cpan:STEVAN';
 
-my %CACHED_PARAM_SPECS;
+my %CACHED_SPECS;
 
 sub validated_hash {
-    my ( $args, %params ) = @_;
+    my ( $args, %spec ) = @_;
 
     my $cache_key;
-    if ( exists $params{MX_PARAMS_VALIDATE_CACHE_KEY} ) {
-        $cache_key = $params{MX_PARAMS_VALIDATE_CACHE_KEY};
-        delete $params{MX_PARAMS_VALIDATE_CACHE_KEY};
+    if ( exists $spec{MX_PARAMS_VALIDATE_CACHE_KEY} ) {
+        $cache_key = $spec{MX_PARAMS_VALIDATE_CACHE_KEY};
+        delete $spec{MX_PARAMS_VALIDATE_CACHE_KEY};
     }
     else {
         $cache_key = ( caller(1) )[3];
     }
 
-    if ( exists $CACHED_PARAM_SPECS{$cache_key} ) {
-        ( ref $CACHED_PARAM_SPECS{$cache_key} eq 'HASH' )
+    if ( exists $CACHED_SPECS{$cache_key} ) {
+        ( ref $CACHED_SPECS{$cache_key} eq 'HASH' )
             || confess
             "I was expecting a HASH-ref in the cached $cache_key parameter"
             . " spec, you are doing something funky, stop it!";
-        %params = %{ $CACHED_PARAM_SPECS{$cache_key} };
+        %spec = %{ $CACHED_SPECS{$cache_key} };
     }
     else {
-        my $should_cache
-            = exists $params{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
-        delete $params{MX_PARAMS_VALIDATE_NO_CACHE};
+        my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
 
-        # prepare the parameters ...
-        $params{$_} = _convert_to_param_validate_spec( $params{$_} )
-            foreach keys %params;
-        $CACHED_PARAM_SPECS{$cache_key} = \%params
+        $spec{$_} = _convert_to_param_validate_spec( $spec{$_} )
+            foreach keys %spec;
+
+        $CACHED_SPECS{$cache_key} = \%spec
             if $should_cache;
     }
 
@@ -57,12 +55,12 @@
 
     my %args = @$args;
 
-    _coerce_args( \%args, \%params )
-        if grep { $params{$_}{coerce} } keys %params;
+    $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} )
+        for grep { $spec{$_}{coerce} } keys %spec;
 
     %args = Params::Validate::validate_with(
         params => \%args,
-        spec   => \%params
+        spec   => \%spec
     );
 
     return ( ( $instance ? $instance : () ), %args );
@@ -71,40 +69,36 @@
 *validate = \&validated_hash;
 
 sub validated_list {
-    my ( $args, @params ) = @_;
+    my ( $args, @spec ) = @_;
 
-    my %params = @params;
+    my %spec = @spec;
 
     my $cache_key;
-    if ( exists $params{MX_PARAMS_VALIDATE_CACHE_KEY} ) {
-        $cache_key = $params{MX_PARAMS_VALIDATE_CACHE_KEY};
-        delete $params{MX_PARAMS_VALIDATE_CACHE_KEY};
+    if ( exists $spec{MX_PARAMS_VALIDATE_CACHE_KEY} ) {
+        $cache_key = delete $spec{MX_PARAMS_VALIDATE_CACHE_KEY};
     }
     else {
         $cache_key = ( caller(1) )[3];
     }
 
-    my @ordered_params;
-    if ( exists $CACHED_PARAM_SPECS{$cache_key} ) {
-        ( ref $CACHED_PARAM_SPECS{$cache_key} eq 'ARRAY' )
+    my @ordered_spec;
+    if ( exists $CACHED_SPECS{$cache_key} ) {
+        ( ref $CACHED_SPECS{$cache_key} eq 'ARRAY' )
             || confess
             "I was expecting a ARRAY-ref in the cached $cache_key parameter"
             . " spec, you are doing something funky, stop it!";
-        %params         = %{ $CACHED_PARAM_SPECS{$cache_key}->[0] };
-        @ordered_params = @{ $CACHED_PARAM_SPECS{$cache_key}->[1] };
+        %spec         = %{ $CACHED_SPECS{$cache_key}->[0] };
+        @ordered_spec = @{ $CACHED_SPECS{$cache_key}->[1] };
     }
     else {
-        my $should_cache
-            = exists $params{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
-        delete $params{MX_PARAMS_VALIDATE_NO_CACHE};
+        my $should_cache = delete $spec{MX_PARAMS_VALIDATE_NO_CACHE} ? 0 : 1;
 
-        @ordered_params = grep { exists $params{$_} } @params;
+        @ordered_spec = grep { exists $spec{$_} } @spec;
 
-        # prepare the parameters ...
-        $params{$_} = _convert_to_param_validate_spec( $params{$_} )
-            foreach keys %params;
+        $spec{$_} = _convert_to_param_validate_spec( $spec{$_} )
+            foreach keys %spec;
 
-        $CACHED_PARAM_SPECS{$cache_key} = [ \%params, \@ordered_params ]
+        $CACHED_SPECS{$cache_key} = [ \%spec, \@ordered_spec ]
             if $should_cache;
     }
 
@@ -113,17 +107,17 @@
 
     my %args = @$args;
 
-    _coerce_args( \%args, \%params )
-        if grep { $params{$_}{coerce} } keys %params;
+    $args{$_} = $spec{$_}{constraint}->coerce( $args{$_} )
+        for grep { $spec{$_}{coerce} } keys %spec;
 
     %args = Params::Validate::validate_with(
         params => \%args,
-        spec   => \%params
+        spec   => \%spec
     );
 
     return (
         ( $instance ? $instance : () ),
-        @args{@ordered_params}
+        @args{@ordered_spec}
     );
 }
 
@@ -151,14 +145,9 @@
         }
         else {
             $constraint
-                = Moose::Util::TypeConstraints::find_or_create_type_constraint(
-                $spec->{isa} => {
-                    parent =>
-                        Moose::Util::TypeConstraints::find_type_constraint(
-                        'Object'),
-                    constraint => sub { $_[0]->isa( $spec->{isa} ) }
-                }
-                );
+                = Moose::Util::TypeConstraints::find_or_parse_type_constraint(
+                $spec->{isa} )
+                || class_type( $spec->{isa} );
         }
 
         $pv_spec{constraint} = $constraint;
@@ -176,17 +165,12 @@
             $constraint = $spec->{does};
         }
         else {
-            $constraint
-                = Moose::Util::TypeConstraints::find_or_create_type_constraint(
-                $spec->{does} => {
-                    parent =>
-                        Moose::Util::TypeConstraints::find_type_constraint(
-                        'Role'),
-                    constraint => sub { $_[0]->does( $spec->{does} ) }
-                }
-                );
+            $constraint = find_type_constraint( $spec->{does} )
+                || role_type( $spec->{does} );
         }
 
+        $pv_spec{constraint} = $constraint;
+
         $pv_spec{callbacks} = {
             'checking type constraint' => sub { $constraint->check( $_[0] ) }
         };
@@ -198,15 +182,6 @@
     return \%pv_spec;
 }
 
-sub _coerce_args {
-    my ( $args, $params ) = @_;
-
-    for my $k ( grep { $params->{$_}{coerce} } keys %{$params} ) {
-        $args->{$k} = $params->{$k}{constraint}->coerce( $args->{$k} );
-    }
-
-}
-
 1;
 
 __END__




More information about the Moose-commits mailing list