Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Counter; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
04e05413 |
5 | with 'Moose::Meta::Attribute::Native::Trait' => |
6 | { -excludes => ['_root_types'] }; |
e3c07b19 |
7 | |
2edb73d9 |
8 | sub _default_default { 0 } |
9 | sub _default_is { 'ro' } |
2e069f5a |
10 | sub _helper_type { 'Num' } |
04e05413 |
11 | sub _root_types { 'Num', 'Int' } |
e3c07b19 |
12 | |
e3c07b19 |
13 | no Moose::Role; |
14 | |
e3c07b19 |
15 | 1; |
16 | |
20544c31 |
17 | # ABSTRACT: Helper trait for Int attributes which represent counters |
ad46f524 |
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 |
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>. |
e3c07b19 |
57 | |
e3c07b19 |
58 | =head1 PROVIDED METHODS |
59 | |
e3c07b19 |
60 | =over 4 |
61 | |
e132fd56 |
62 | =item * B<set($value)> |
e3c07b19 |
63 | |
e132fd56 |
64 | Sets the counter to the specified value and returns the new value. |
e3c07b19 |
65 | |
e132fd56 |
66 | This method requires a single argument. |
e3c07b19 |
67 | |
e132fd56 |
68 | =item * B<inc> |
e3c07b19 |
69 | |
e132fd56 |
70 | =item * B<inc($arg)> |
e3c07b19 |
71 | |
e132fd56 |
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. |
e3c07b19 |
74 | |
e132fd56 |
75 | This method accepts a single argument. |
e3c07b19 |
76 | |
e132fd56 |
77 | =item * B<dec> |
e3c07b19 |
78 | |
e132fd56 |
79 | =item * B<dec($arg)> |
e3c07b19 |
80 | |
e132fd56 |
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. |
7250a968 |
83 | |
e132fd56 |
84 | This method accepts a single argument. |
85 | |
86 | =item * B<reset> |
7250a968 |
87 | |
e132fd56 |
88 | Resets the value stored in this slot to its default value, and returns the new |
89 | value. |
7250a968 |
90 | |
7250a968 |
91 | =back |
92 | |
e3c07b19 |
93 | =head1 BUGS |
94 | |
d4048ef3 |
95 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
96 | |
e3c07b19 |
97 | =cut |