From: Cory G Watson Date: Fri, 20 Jun 2008 19:15:20 +0000 (+0000) Subject: Add set method to Counter and let inc/dec take args. X-Git-Tag: 0.16~52 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eef0ea50d49816a68fce54dbb8162926e90d0074;p=gitmo%2FMooseX-AttributeHelpers.git Add set method to Counter and let inc/dec take args. --- diff --git a/ChangeLog b/ChangeLog index 754ef60..3f07ffb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ Revision history for Perl extension MooseX-AttributeHelpers 0.10 + - Counter: add set and allow inc and dec to accept args - add Bool as an attribute helper (thanks to jasonmay) 0.09 Sat. May, 24, 2008 diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index 2da3971..7efbb51 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -122,17 +122,23 @@ modification of the value stored in the attribute. =over 4 +=item I + +Set the counter to the specified value. + =item I -Increments the value stored in this slot by 1. +Increments the value stored in this slot by 1.Providing an argument will +cause the counter to be increased by specified amount. =item I -Decrements the value stored in this slot by 1. +Decrements the value stored in this slot by 1. Providing an argument will +cause the counter to be increased by specified amount. =item I -Resets the value stored in this slot to it's default value. +Resets the value stored in this slot to it's default value. =back diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm index 8a9b09d..84b978f 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm @@ -5,19 +5,24 @@ use Moose::Role; our $VERSION = '0.02'; our $AUTHORITY = 'cpan:STEVAN'; -sub reset : method { +sub reset : method { my ($attr, $reader, $writer) = @_; return sub { $writer->($_[0], $attr->default($_[0])) }; } +sub set : method { + my ($attr, $reader, $writer, $value) = @_; + return sub { $writer->($_[0], $_[1]) }; +} + sub inc { my ($attr, $reader, $writer) = @_; - return sub { $writer->($_[0], $reader->($_[0]) + 1) }; + return sub { $writer->($_[0], $reader->($_[0]) + (defined($_[1]) ? $_[1] : 1) ) }; } sub dec { my ($attr, $reader, $writer) = @_; - return sub { $writer->($_[0], $reader->($_[0]) - 1) }; + return sub { $writer->($_[0], $reader->($_[0]) - (defined($_[1]) ? $_[1] : 1) ) }; } 1; @@ -47,6 +52,8 @@ L. =over 4 +=item B + =item B =item B diff --git a/t/001_basic_counter.t b/t/001_basic_counter.t index 1f7d760..b94c0a5 100644 --- a/t/001_basic_counter.t +++ b/t/001_basic_counter.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 18; BEGIN { use_ok('MooseX::AttributeHelpers'); @@ -22,6 +22,7 @@ BEGIN { inc => 'inc_counter', dec => 'dec_counter', reset => 'reset_counter', + set => 'set_counter' } ); } @@ -33,6 +34,7 @@ can_ok($page, $_) for qw[ dec_counter inc_counter reset_counter + set_counter ]; is($page->counter, 0, '... got the default value'); @@ -49,6 +51,15 @@ is($page->counter, 1, '... got the decremented value'); $page->reset_counter; is($page->counter, 0, '... got the original value'); +$page->set_counter(5); +is($page->counter, 5, '... set the value'); + +$page->inc_counter(2); +is($page->counter, 7, '... increment by arg'); + +$page->dec_counter(5); +is($page->counter, 2, '... decrement by arg'); + # check the meta .. my $counter = $page->meta->get_attribute('counter'); @@ -61,6 +72,7 @@ is($counter->type_constraint->name, 'Int', '... got the expected type constraint is_deeply($counter->provides, { inc => 'inc_counter', dec => 'dec_counter', - reset => 'reset_counter', + reset => 'reset_counter', + set => 'set_counter' }, '... got the right provides methods');