From: Shawn M Moore Date: Tue, 10 Jun 2008 03:55:13 +0000 (+0000) Subject: Moose compat: the weak_ref reader is spelled is_weak_ref, don't weaken nonreferences X-Git-Tag: 0.04~56 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=3645b3164ce4e4fafa8ee65c515281175da0efe7 Moose compat: the weak_ref reader is spelled is_weak_ref, don't weaken nonreferences --- diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index 115af44..0265e1d 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -26,7 +26,7 @@ sub is_lazy { $_[0]->{lazy} } sub predicate { $_[0]->{predicate} } sub clearer { $_[0]->{clearer} } sub handles { $_[0]->{handles} } -sub weak_ref { $_[0]->{weak_ref} } +sub is_weak_ref { $_[0]->{weak_ref} } sub init_arg { $_[0]->{init_arg} } sub type_constraint { $_[0]->{type_constraint} } sub trigger { $_[0]->{trigger} } @@ -67,8 +67,8 @@ sub generate_accessor { $accessor .= '$self->{$key} = $_;'; - if ($attribute->weak_ref) { - $accessor .= 'Scalar::Util::weaken($self->{$key});'; + if ($attribute->is_weak_ref) { + $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});'; } if ($trigger) { @@ -269,7 +269,7 @@ installed. Some error checking is done. =head2 has_handles -> Bool -=head2 weak_ref -> Bool +=head2 is_weak_ref -> Bool =head2 init_arg -> Str diff --git a/lib/Mouse/Object.pm b/lib/Mouse/Object.pm index 4af0f39..fbd19ed 100644 --- a/lib/Mouse/Object.pm +++ b/lib/Mouse/Object.pm @@ -33,7 +33,7 @@ sub new { $instance->{$key} = $value; weaken($instance->{$key}) - if $attribute->weak_ref; + if ref($instance->{$key}) && $attribute->is_weak_ref; } } else { @@ -50,7 +50,7 @@ sub new { $instance->{$key} = $args{$key}; weaken($instance->{$key}) - if $attribute->weak_ref; + if ref($instance->{$key}) && $attribute->is_weak_ref; if ($attribute->has_trigger) { $attribute->trigger->($instance, $args{$key}, $attribute); diff --git a/t/021-weak-ref.t b/t/021-weak-ref.t index bb3bf6f..2276724 100644 --- a/t/021-weak-ref.t +++ b/t/021-weak-ref.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 18; +use Test::More tests => 21; use Test::Exception; use Scalar::Util 'isweak'; @@ -44,22 +44,33 @@ is($destroyed{accessor}, 1, "destroyed from the accessor"); is($destroyed{constructor}, 1, "destroyed from the constructor"); is($destroyed{middle}, 1, "casuality of war"); -ok(!Class->meta->get_attribute('type')->weak_ref, "type is not a weakref"); -ok(Class->meta->get_attribute('self')->weak_ref, "self IS a weakref"); +ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref"); +ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref"); do { package Class2; use Mouse; has value => ( - is => 'ro', + is => 'rw', default => 10, weak_ref => 1, ); }; -throws_ok { Class2->new } qr/Can't weaken a nonreference/; -ok(Class2->meta->get_attribute('value')->weak_ref, "value IS a weakref"); +ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref"); + +lives_ok { + my $obj = Class2->new; + is($obj->value, 10, "weak_ref doesn't apply to non-refs"); +}; + +my $obj2 = Class2->new; +lives_ok { + $obj2->value({}); +}; + +is_deeply($obj2->value, undef, "weakened the reference even with a nonref default"); do { package Class3; @@ -81,4 +92,4 @@ $obj->hashref({1 => 1}); is($obj->hashref, undef, "hashref collected between set and get because refcount=0"); ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance'); -ok(Class3->meta->get_attribute('hashref')->weak_ref, "hashref IS a weakref"); +ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");