X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FTypeConstraint.pm;h=3e370dc70385052d5286c162de2e415e41b4488e;hb=25a19dffe45b4a14ae23a4aea9b91e872f78e825;hp=b4cfec5cd9adcba763ddd2a292c2eb200485a4a9;hpb=ad46f5244f59757c45306c4a41e195b7aa4b0943;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/TypeConstraint.pm b/lib/Moose/Meta/TypeConstraint.pm index b4cfec5..3e370dc 100644 --- a/lib/Moose/Meta/TypeConstraint.pm +++ b/lib/Moose/Meta/TypeConstraint.pm @@ -12,11 +12,10 @@ use overload '0+' => sub { refaddr(shift) }, # id an object use Scalar::Util qw(blessed refaddr); use Sub::Name qw(subname); +use Try::Tiny; use base qw(Class::MOP::Object); -our $AUTHORITY = 'cpan:STEVAN'; - __PACKAGE__->meta->add_attribute('name' => (reader => 'name')); __PACKAGE__->meta->add_attribute('parent' => ( reader => 'parent', @@ -44,6 +43,11 @@ __PACKAGE__->meta->add_attribute('hand_optimized_type_constraint' => ( predicate => 'has_hand_optimized_type_constraint', )); +__PACKAGE__->meta->add_attribute('inlined' => ( + accessor => 'inlined', + predicate => 'has_inlined_type_constraint', +)); + sub parents { my $self; $self->parent; @@ -123,6 +127,15 @@ sub validate { } } +sub _inline_check { + my $self = shift; + + die 'Cannot inline a type constraint check for ' . $self->name + unless $self->has_inlined_type_constraint; + + return $self->inlined->( $self, @_ ); +} + sub assert_valid { my ($self, $value) = @_; @@ -140,7 +153,18 @@ sub get_message { return $msg->($value); } else { - $value = (defined $value ? overload::StrVal($value) : 'undef'); + # have to load it late like this, since it uses Moose itself + my $can_partialdump = try { + # versions prior to 0.14 had a potential infinite loop bug + Class::MOP::load_class('Devel::PartialDump', { -version => 0.14 }); + 1; + }; + if ($can_partialdump) { + $value = Devel::PartialDump->new->dump($value); + } + else { + $value = (defined $value ? overload::StrVal($value) : 'undef'); + } return "Validation failed for '" . $self->name . "' with value $value"; } } @@ -229,7 +253,6 @@ sub _compile_hand_optimized_type_constraint { unless ( ref $type_constraint ) { require Moose; - Carp::confess ("Hand optimized type constraint for " . $self->name . " is not a code reference"); Moose->throw_error("Hand optimized type constraint is not a code reference"); }