X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FAttribute.pm;h=4c248501b1835ca425f0adf52174db5a30231438;hb=926290ac173b77f0ff0027dcbd95277c9cc2be54;hp=1ef8a77802ce88c363afb7a19b85d9ce548a04c5;hpb=bedd575c2018fe372304d9c03b1e9427435c9622;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index 1ef8a77..4c24850 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -142,11 +142,11 @@ sub new { $args{name} = $name; - my $instance = bless \%args, $class; + my $self = bless \%args, $class; # extra attributes if($class ne __PACKAGE__){ - $class->meta->_initialize_instance($instance,\%args); + $class->meta->_initialize_object($self, \%args); } # XXX: there is no fast way to check attribute validity @@ -156,7 +156,7 @@ sub new { # Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad"); # } - return $instance + return $self; } # readers @@ -265,15 +265,32 @@ sub create { return $self; } +sub _coerce_and_verify { + my($self, $value, $instance) = @_; + + my $type_constraint = $self->{type_constraint}; + + return $value if !$type_constraint; + + if ($self->should_coerce && $type_constraint->has_coercion) { + $value = $type_constraint->coerce($value); + } + + return $value if $type_constraint->check($value); + + $self->verify_against_type_constraint($value); + + return $value; +} + sub verify_against_type_constraint { my ($self, $value) = @_; - my $tc = $self->type_constraint; - return 1 unless $tc; - local $_ = $value; - return 1 if $tc->check($value); + my $type_constraint = $self->{type_constraint}; + return 1 if !$type_constraint;; + return 1 if $type_constraint->check($value); - $self->verify_type_constraint_error($self->name, $value, $tc); + $self->verify_type_constraint_error($self->name, $value, $type_constraint); } sub verify_type_constraint_error { @@ -284,6 +301,9 @@ sub verify_type_constraint_error { sub coerce_constraint { ## my($self, $value) = @_; my $type = $_[0]->{type_constraint} or return $_[1]; + + Carp::cluck("coerce_constraint() has been deprecated, which was an internal utility anyway"); + return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $type, $_[1]); }