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
=over 4
+=item I<set>
+
+Set the counter to the specified value.
+
=item I<inc>
-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<dec>
-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<reset>
-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
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;
=over 4
+=item B<set>
+
=item B<inc>
=item B<dec>
use strict;
use warnings;
-use Test::More tests => 14;
+use Test::More tests => 18;
BEGIN {
use_ok('MooseX::AttributeHelpers');
inc => 'inc_counter',
dec => 'dec_counter',
reset => 'reset_counter',
+ set => 'set_counter'
}
);
}
dec_counter
inc_counter
reset_counter
+ set_counter
];
is($page->counter, 0, '... got the default 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');
is_deeply($counter->provides, {
inc => 'inc_counter',
dec => 'dec_counter',
- reset => 'reset_counter',
+ reset => 'reset_counter',
+ set => 'set_counter'
}, '... got the right provides methods');