From: Hans Dieter Pearcey Date: Mon, 30 Mar 2009 05:08:24 +0000 (-0400) Subject: fix RT#44429 after discussion on the mailing list X-Git-Tag: 0.73_01~26 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c2685d2054e3f63cf38fca4fad1074b4a2e5be83;p=gitmo%2FMoose.git fix RT#44429 after discussion on the mailing list --- diff --git a/Changes b/Changes index d6f523e..d9e5827 100644 --- a/Changes +++ b/Changes @@ -30,6 +30,17 @@ for, noteworthy changes. - Make init_meta() examples explicitly return the metaclass and point out this fact. (hdp) + * Moose::Meta::Class + * Moose::Meta::Method::Constructor + - Attribute triggers now consistently do not receive the meta-attribute + object as an argument. Previously, triggers called during instance + construction were passed the meta-attribute, but triggers called by + normal accessors were not. Fixes RT#44429, reported by Mark Swayne. + (hdp) + + * Moose::Manual::Attributes + - Remove references to triggers receving the meta-attribute object as an + argument. (hdp) 0.73 Fri, March 29, 2009 * No changes from 0.72_01. diff --git a/lib/Moose/Manual/Attributes.pod b/lib/Moose/Manual/Attributes.pod index 8ed0258..7a2963b 100644 --- a/lib/Moose/Manual/Attributes.pod +++ b/lib/Moose/Manual/Attributes.pod @@ -448,14 +448,13 @@ set: ); sub _size_set { - my ( $self, $size, $meta_attr ) = @_; + my ( $self, $size ) = @_; warn $self->name, " size is now $size\n"; } -The trigger is called as a method, and receives the new value as well -as the L object for the attribute. The trigger -is called I the value is set. +The trigger is called as a method, and receives the new value as its argument. +The trigger is called I the value is set. This differs from an after method modifier in two ways. First, a trigger is only called when the attribute is set, as opposed to diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 73256ec..d76da79 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -181,7 +181,6 @@ sub new_object { ? $attr->get_read_method_ref->($self) : $params->{$init_arg} ), - $attr ); } diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index c035ac6..1830e5c 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -244,8 +244,7 @@ sub _generate_triggers { $attr->name, ) . ', ' - . '$attrs->[' - . $i . ']' . ');' . "\n}"; + . ');' . "\n}"; } return join ";\n" => @trigger_calls; diff --git a/t/020_attributes/004_attribute_triggers.t b/t/020_attributes/004_attribute_triggers.t index 637d604..859607f 100644 --- a/t/020_attributes/004_attribute_triggers.t +++ b/t/020_attributes/004_attribute_triggers.t @@ -5,7 +5,7 @@ use warnings; use Scalar::Util 'isweak'; -use Test::More tests => 36; +use Test::More tests => 40; use Test::Exception; @@ -158,3 +158,33 @@ use Test::Exception; is_deeply(\%Blarg::trigger_vals, { map { $_ => "Yet another $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values'); } +# Triggers receive the meta-attribute as an argument + +{ + package Foo; + use Moose; + our @calls; + has foo => (is => 'rw', trigger => sub { push @calls, [@_] }); +} + +{ + my $attr = Foo->meta->get_attribute('foo'); + my $foo = Foo->new(foo => 2); + is_deeply( + \@Foo::calls, + [ [ $foo, 2 ] ], + 'trigger called correctly on construction', + ); + @Foo::calls = (); + + $foo->foo(3); + is_deeply( + \@Foo::calls, + [ [ $foo, 3 ] ], + 'trigger called correctly on set', + ); + @Foo::calls = (); + Foo->meta->make_immutable, redo if Foo->meta->is_mutable; +} + +