Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

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

People
Owner: Nobody in particular
Requestors: notbenh [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: it would be nice to have a re-default method much like clearer
With clearer setting everything to undef, you run in to issues with non-undefable data types. It would be nice to have a re-default so that you could do a rollback of an object. Currently I just re-set attr to the default value (example below) but I'm sure that there's a smarter way to go about it. #!/usr/bin/perl use strict; use warnings; use Test::Most qw{no_plan}; BEGIN { package My::Test; use Moose; has int => ( is => 'rw', isa => 'Int', default => 10, ); has hash => ( is => 'rw', isa => 'HashRef', default => sub{{}}, ); sub redef { my ($s,$n)=@_; my $d = $s->meta->find_attribute_by_name($n)->default; $d = $d->() if ref($d) eq 'CODE'; $s->$n( $d ); } } #END BEGIN my $t = My::Test->new; is( $t->int, 10 ); ok( $t->int(100) ); is( $t->int, 100 ); ok( $t->redef('int') ); is( $t->int, 10 ); is_deeply( $t->hash, {} ); ok( $t->hash({ qw{hello world} })); is_deeply( $t->hash, { qw{hello world} } ); ok( $t->redef('hash') ); is_deeply( $t->hash, {} ); -- benh~
Benh, You can use a combination of a clearer with a lazy default and get the same behavior. - Stevan #!/usr/bin/perl use strict; use warnings; use Test::More qw{no_plan}; BEGIN { package My::Test; use Moose; has int => ( is => 'rw', isa => 'Int', lazy => 1, default => 10, clearer => 'clear_int', ); has hash => ( is => 'rw', isa => 'HashRef', default => sub{{}}, lazy => 1, clearer => 'clear_hash', ); } #END BEGIN my $t = My::Test->new; is( $t->int, 10 ); ok( $t->int(100) ); is( $t->int, 100 ); ok( $t->clear_int ); is( $t->int, 10 ); is_deeply( $t->hash, {} ); ok( $t->hash({ qw{hello world} })); is_deeply( $t->hash, { qw{hello world} } ); ok( $t->clear_hash ); is_deeply( $t->hash, {} );
Marking this resolved, since lazy + default is the right solution. No need for a new feature.