[Moose-commits] r7956 - in MooseX-Params-Validate/trunk:
lib/MooseX/Params t
autarch at code2.0beta.co.uk
autarch at code2.0beta.co.uk
Tue Jul 7 18:30:25 GMT 2009
Author: autarch
Date: 2009-07-07 11:30:24 -0700 (Tue, 07 Jul 2009)
New Revision: 7956
Modified:
MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
MooseX-Params-Validate/trunk/t/001_basic.t
Log:
allow callbacks in spec
Modified: MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm
===================================================================
--- MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm 2009-07-07 17:52:28 UTC (rev 7955)
+++ MooseX-Params-Validate/trunk/lib/MooseX/Params/Validate.pm 2009-07-07 18:30:24 UTC (rev 7956)
@@ -180,46 +180,30 @@
$pv_spec{coerce} = $spec->{coerce}
if exists $spec->{coerce};
- if ( exists $spec->{isa} ) {
- my $constraint;
-
- if ( blessed( $spec->{isa} )
- && $spec->{isa}->isa('Moose::Meta::TypeConstraint') ) {
- $constraint = $spec->{isa};
- }
- else {
- $constraint
- = Moose::Util::TypeConstraints::find_or_parse_type_constraint(
- $spec->{isa} )
- || class_type( $spec->{isa} );
- }
-
- $pv_spec{constraint} = $constraint;
-
- $pv_spec{callbacks} = {
- 'checking type constraint for '
- . $constraint->name => sub { $constraint->check( $_[0] ) }
- };
+ my $constraint;
+ if ( defined $spec->{isa} ) {
+ $constraint
+ = _is_tc( $spec->{isa} )
+ || Moose::Util::TypeConstraints::find_or_parse_type_constraint(
+ $spec->{isa} )
+ || class_type( $spec->{isa} );
}
- elsif ( exists $spec->{does} ) {
+ elsif ( defined $spec->{does} ) {
+ $constraint
+ = _is_tc( $spec->{isa} )
+ || find_type_constraint( $spec->{does} )
+ || role_type( $spec->{does} );
+ }
- my $constraint;
+ $pv_spec{callbacks} = $spec->{callbacks}
+ if exists $spec->{callbacks};
- if ( blessed( $spec->{does} )
- && $spec->{does}->isa('Moose::Meta::TypeConstraint') ) {
- $constraint = $spec->{does};
- }
- else {
- $constraint = find_type_constraint( $spec->{does} )
- || role_type( $spec->{does} );
- }
-
+ if ($constraint) {
$pv_spec{constraint} = $constraint;
- $pv_spec{callbacks} = {
- 'checking type constraint for '
- . $constraint->name => sub { $constraint->check( $_[0] ) }
- };
+ $pv_spec{callbacks}
+ { 'checking type constraint for ' . $constraint->name }
+ = sub { $constraint->check( $_[0] ) };
}
delete $pv_spec{coerce}
@@ -228,6 +212,15 @@
return \%pv_spec;
}
+sub _is_tc {
+ my $maybe_tc = shift;
+
+ return $maybe_tc
+ if defined $maybe_tc
+ && blessed $maybe_tc
+ && $maybe_tc->isa('Moose::Meta::TypeConstraint');
+}
+
sub _caller_name {
my $depth = shift || 0;
Modified: MooseX-Params-Validate/trunk/t/001_basic.t
===================================================================
--- MooseX-Params-Validate/trunk/t/001_basic.t 2009-07-07 17:52:28 UTC (rev 7955)
+++ MooseX-Params-Validate/trunk/t/001_basic.t 2009-07-07 18:30:24 UTC (rev 7956)
@@ -3,7 +3,7 @@
use strict;
use warnings;
-use Test::More tests => 33;
+use Test::More tests => 35;
use Test::Exception;
{
@@ -56,6 +56,21 @@
);
return $params{foo} || $params{bar} || $params{boo};
}
+
+ sub quux {
+ my $self = shift;
+ my %params = validated_hash(
+ \@_,
+ foo => {
+ isa => 'ArrayRef',
+ callbacks => {
+ 'some random callback' => sub { @{ $_[0] } <= 2 },
+ },
+ },
+ );
+
+ return $params{foo};
+ }
}
my $foo = Foo->new;
@@ -156,3 +171,10 @@
qr/\QThe 'gorch' parameter/,
'... gorch requires a ArrayRef[Int]';
+throws_ok { $foo->quux( foo => '123456790' ) }
+qr/\QThe 'foo' parameter\E.+\Qchecking type constraint/,
+'... foo parameter must be an ArrayRef';
+
+throws_ok { $foo->quux( foo => [ 1, 2, 3, 4 ] ) }
+qr/\QThe 'foo' parameter\E.+\Qsome random callback/,
+'... foo parameter additional callback requires that arrayref be 0-2 elements';
More information about the Moose-commits
mailing list