Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 54785
Status: resolved
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: david.tulloh [...] AirservicesAustralia.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.93
Fixed in: 1.15



Subject: Array traits don't trigger trigger function
I would like to have an attribute with an Array trait providing the push method. I would also like to have a trigger function called whenever the attribute is modified. Both of these work independantly however modifying the attribute via the push method does not cause the trigger to be run.
David, Triggers are only fired when the entire value of the attribute is changed, pushing onto an array does not meet this requirement. What you want is to put an "after" method modifier on the push method, like so: has 'foo' => ( traits => [ 'Array' ], is => 'ro', isa => 'ArrayRef[ Foo ]', lazy => 1, default => sub { [] }, handles => { add_foo => 'push' } ); after 'foo' => sub { my ($self, @foos) = @_; if (@foos) { # ... code you want to trigger here } } - Stevan On Thu Feb 18 22:18:20 2010, david.tulloh@AirservicesAustralia.com wrote: Show quoted text
> I would like to have an attribute with an Array trait providing the push > method. > > I would also like to have a trigger function called whenever the > attribute is modified. > > Both of these work independantly however modifying the attribute via the > push method does not cause the trigger to be run.
David, Sorry, the "after" modifier should be in the "add_foos" method, not "foos". - Stevan On Fri Feb 19 09:45:00 2010, STEVAN wrote: Show quoted text
> David, > > Triggers are only fired when the entire value of the attribute is > changed, pushing onto an > array does not meet this requirement. > > What you want is to put an "after" method modifier on the push method, > like so: > > has 'foo' => ( > traits => [ 'Array' ], > is => 'ro', > isa => 'ArrayRef[ Foo ]', > lazy => 1, > default => sub { [] }, > handles => { > add_foo => 'push' > } > ); > > after 'foo' => sub { > my ($self, @foos) = @_; > if (@foos) { > # ... code you want to trigger here > } > } > > > - Stevan > > On Thu Feb 18 22:18:20 2010, david.tulloh@AirservicesAustralia.com > wrote:
> > I would like to have an attribute with an Array trait providing the
> push
> > method. > > > > I would also like to have a trigger function called whenever the > > attribute is modified. > > > > Both of these work independantly however modifying the attribute via
> the
> > push method does not cause the trigger to be run.
>
Subject: Re: [rt.cpan.org #54785] Array traits don't trigger trigger function
Date: Fri, 19 Feb 2010 10:57:27 -0500
To: bug-Moose [...] rt.cpan.org
From: Chris Prather <perigrin [...] gmail.com>
On Fri, Feb 19, 2010 at 9:45 AM, Stevan Little via RT <bug-Moose@rt.cpan.org> wrote: Show quoted text
>       Queue: Moose >  Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=54785 > > > David, > > Triggers are only fired when the entire value of the attribute is changed, pushing onto an > array does not meet this requirement. > > What you want is to put an "after" method modifier on the push method, like so: > > has 'foo' => ( >    traits  => [ 'Array' ], >    is      => 'ro', >    isa     => 'ArrayRef[ Foo ]', >    lazy    => 1, >    default => sub { [] }, >    handles => { >        add_foo => 'push' >    } > ); > > after 'foo' => sub { >    my ($self, @foos) = @_; >    if (@foos) { >        # ... code you want to trigger here >    } > }
This works better if you say after 'add_foo' mind you. -Chris
From: david.tulloh [...] AirservicesAustralia.com
On Fri Feb 19 09:45:00 2010, STEVAN wrote: Show quoted text
> David, > > Triggers are only fired when the entire value of the attribute is > changed, pushing onto an > array does not meet this requirement. > > What you want is to put an "after" method modifier on the push method, > like so: > ...
Thanks for the response. It had never occured to me to apply a method modifier to a trait provided method. Could I suggest that the documentation be updated to make this trigger requirement clear. The Manual Attributes documentation is quite specific on when triggers are fired and from my reading of it I expected it to be fired whenever the value was changed. The Trait Array documentation doesn't mention triggers at all.