one more package to hide
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Counter.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait::Counter;
e3c07b19 3use Moose::Role;
4
04e05413 5with 'Moose::Meta::Attribute::Native::Trait' =>
6 { -excludes => ['_root_types'] };
e3c07b19 7
2edb73d9 8sub _default_default { 0 }
9sub _default_is { 'ro' }
2e069f5a 10sub _helper_type { 'Num' }
04e05413 11sub _root_types { 'Num', 'Int' }
e3c07b19 12
e3c07b19 13no Moose::Role;
14
e3c07b19 151;
16
ad46f524 17# ABSTRACT: Helper trait for counters
18
e3c07b19 19__END__
20
21=pod
22
e3c07b19 23=head1 SYNOPSIS
24
25 package MyHomePage;
26 use Moose;
e3c07b19 27
28 has 'counter' => (
e132fd56 29 traits => ['Counter'],
30 is => 'ro',
31 isa => 'Num',
32 default => 0,
33 handles => {
5f3663b2 34 inc_counter => 'inc',
35 dec_counter => 'dec',
36 reset_counter => 'reset',
9610c1d2 37 },
e3c07b19 38 );
39
40 my $page = MyHomePage->new();
e132fd56 41 $page->inc_counter; # same as $page->counter( $page->counter + 1 );
42 $page->dec_counter; # same as $page->counter( $page->counter - 1 );
43
aa276b31 44 my $count_by_twos = 2;
45 $page->inc_counter($count_by_twos);
e3c07b19 46
47=head1 DESCRIPTION
48
7795e4de 49This trait provides native delegation methods for counters. A counter can be
50any sort of number (integer or not). The delegation methods allow you to
51increment, decrement, or reset the value.
52
53=head1 DEFAULT TYPE
54
55If you don't provide an C<isa> value for your attribute, it will default to
56C<Num>.
e3c07b19 57
e3c07b19 58=head1 PROVIDED METHODS
59
e3c07b19 60=over 4
61
e132fd56 62=item * B<set($value)>
e3c07b19 63
e132fd56 64Sets the counter to the specified value and returns the new value.
e3c07b19 65
e132fd56 66This method requires a single argument.
e3c07b19 67
e132fd56 68=item * B<inc>
e3c07b19 69
e132fd56 70=item * B<inc($arg)>
e3c07b19 71
e132fd56 72Increases the attribute value by the amount of the argument, or by 1 if no
73argument is given. This method returns the new value.
e3c07b19 74
e132fd56 75This method accepts a single argument.
e3c07b19 76
e132fd56 77=item * B<dec>
e3c07b19 78
e132fd56 79=item * B<dec($arg)>
e3c07b19 80
e132fd56 81Decreases the attribute value by the amount of the argument, or by 1 if no
82argument is given. This method returns the new value.
7250a968 83
e132fd56 84This method accepts a single argument.
85
86=item * B<reset>
7250a968 87
e132fd56 88Resets the value stored in this slot to its default value, and returns the new
89value.
7250a968 90
7250a968 91=back
92
e3c07b19 93=head1 BUGS
94
d4048ef3 95See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 96
e3c07b19 97=cut