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