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