X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FAttribute.pm;fp=lib%2FMouse%2FAttribute.pm;h=36c907a4546f141083f92249e156bb2e112ab078;hp=da448b50bb38bf0fa30c1905513e1a6af2ab5401;hb=5aa30cedaeea188c11688187a689d70f4b7698d2;hpb=ccea8101f730f46ee7e85769b6472cf91d0f5240 diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index da448b5..36c907a 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -44,15 +44,7 @@ sub generate_accessor { my $default = $attribute->{default}; my $trigger = $attribute->{trigger}; my $type = $attribute->{type_constraint}; - - my $constraint = sub { - return unless $type; - - my $checker = Mouse::TypeRegistry->optimized_constraints->{$type}; - return $checker if $checker; - - confess "Unable to parse type constraint '$type'"; - }->(); + my $constraint = $attribute->find_type_constraint; my $accessor = 'sub { my $self = shift;'; @@ -187,6 +179,33 @@ sub create { return $attribute; } +sub find_type_constraint { + my $self = shift; + my $type = $self->type_constraint; + + return unless $type; + + my $checker = Mouse::TypeRegistry->optimized_constraints->{$type}; + return $checker if $checker; + + confess "Unable to parse type constraint '$type'"; +} + +sub verify_type_constraint { + my $self = shift; + local $_ = shift; + + my $type = $self->type_constraint + or return 1; + my $constraint = $self->find_type_constraint + or return 1; + + return 1 if $constraint->($_); + + my $name = $self->name; + Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_"); +} + 1; __END__