From: Shawn M Moore Date: Wed, 4 Jun 2008 04:03:14 +0000 (+0000) Subject: Check type constraint in the setter X-Git-Tag: 0.04~96 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a707f5877879f294bbaa3d0744ddd021c6a2ca9b;p=gitmo%2FMouse.git Check type constraint in the setter --- diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index 22e86e0..4c2caf0 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -29,23 +29,40 @@ sub type_constraint { $_[0]->{type_constraint} } sub generate_accessor { my $attribute = shift; - my $key = $attribute->{init_arg}; - my $default = $attribute->{default}; - my $trigger = $attribute->{trigger}; + my $name = $attribute->{name}; + my $key = $attribute->{init_arg}; + 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 $accessor = 'sub { my $self = shift;'; if ($attribute->{is} eq 'rw') { $accessor .= 'if (@_) { - $self->{$key} = $_[0];'; + local $_ = $_[0];'; + + if ($constraint) { + $accessor .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") unless $constraint->();' + } + + $accessor .= '$self->{$key} = $_;'; if ($attribute->{weak_ref}) { $accessor .= 'Scalar::Util::weaken($self->{$key});'; } if ($trigger) { - $accessor .= '$trigger->($self, $_[0], $attribute);'; + $accessor .= '$trigger->($self, $_, $attribute);'; } $accessor .= '}';