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