From: Jesse Luehrs Date: Sun, 24 Apr 2011 19:36:07 +0000 (-0500) Subject: make the 'reset' delegation for Counter respect builders X-Git-Tag: 2.0100~192 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=dccb4cc6f1de14031631afb63205cbea52e007ab;p=gitmo%2FMoose.git make the 'reset' delegation for Counter respect builders --- diff --git a/Changes b/Changes index 9dc34ef..af30c84 100644 --- a/Changes +++ b/Changes @@ -14,6 +14,9 @@ for, noteworthy changes. * Stop hiding warnings produced by throwing errors in DEMOLISH methods. + * The 'reset' native delegation for Counter attributes will now also respect + builders (previously, it only respected defaults). + 2.0001 Fri, Apr 22, 2011 [ENHANCEMENTS] diff --git a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm index 36319ee..4689c51 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Counter/reset.pm @@ -20,14 +20,21 @@ sub _potential_value { my $self = shift; my ($slot_access) = @_; - return '$attr->default($self)'; + my $attr = $self->associated_attribute; + + return '(do { ' + . join(' ', $attr->_inline_generate_default( + '$self', '$default_for_reset' + )) . ' ' + . '$default_for_reset; ' + . '})'; } sub _inline_optimized_set_new_value { my $self = shift; my ($inv, $new, $slot_access) = @_; - return $slot_access . ' = $attr->default($self);'; + return $slot_access . ' = ' . $self->_potential_value . ';'; } no Moose::Role; diff --git a/t/native_traits/trait_counter.t b/t/native_traits/trait_counter.t index ad2e70d..a314755 100644 --- a/t/native_traits/trait_counter.t +++ b/t/native_traits/trait_counter.t @@ -132,4 +132,41 @@ sub run_tests { $class; } +{ + package WithBuilder; + use Moose; + + has nonlazy => ( + traits => ['Counter'], + is => 'rw', + isa => 'Int', + builder => '_builder', + handles => { + reset_nonlazy => 'reset', + }, + ); + + has lazy => ( + traits => ['Counter'], + is => 'rw', + isa => 'Int', + lazy => 1, + builder => '_builder', + handles => { + reset_lazy => 'reset', + }, + ); + + sub _builder { 1 } +} + +for my $attr ('lazy', 'nonlazy') { + my $obj = WithBuilder->new; + is($obj->$attr, 1, "built properly"); + $obj->$attr(0); + is($obj->$attr, 0, "can be manually set"); + $obj->${\"reset_$attr"}; + is($obj->$attr, 1, "reset resets it to its default value"); +} + done_testing;