Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Counter; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
e3c07b19 |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
04e05413 |
7 | with 'Moose::Meta::Attribute::Native::Trait' => |
8 | { -excludes => ['_root_types'] }; |
e3c07b19 |
9 | |
2edb73d9 |
10 | sub _default_default { 0 } |
11 | sub _default_is { 'ro' } |
2e069f5a |
12 | sub _helper_type { 'Num' } |
04e05413 |
13 | sub _root_types { 'Num', 'Int' } |
e3c07b19 |
14 | |
e3c07b19 |
15 | no Moose::Role; |
16 | |
e3c07b19 |
17 | 1; |
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 |
51 | This trait provides native delegation methods for counters. A counter can be |
52 | any sort of number (integer or not). The delegation methods allow you to |
53 | increment, decrement, or reset the value. |
54 | |
55 | =head1 DEFAULT TYPE |
56 | |
57 | If you don't provide an C<isa> value for your attribute, it will default to |
58 | C<Num>. |
e3c07b19 |
59 | |
e3c07b19 |
60 | =head1 PROVIDED METHODS |
61 | |
e3c07b19 |
62 | =over 4 |
63 | |
e132fd56 |
64 | =item * B<set($value)> |
e3c07b19 |
65 | |
e132fd56 |
66 | Sets the counter to the specified value and returns the new value. |
e3c07b19 |
67 | |
e132fd56 |
68 | This method requires a single argument. |
e3c07b19 |
69 | |
e132fd56 |
70 | =item * B<inc> |
e3c07b19 |
71 | |
e132fd56 |
72 | =item * B<inc($arg)> |
e3c07b19 |
73 | |
e132fd56 |
74 | Increases the attribute value by the amount of the argument, or by 1 if no |
75 | argument is given. This method returns the new value. |
e3c07b19 |
76 | |
e132fd56 |
77 | This method accepts a single argument. |
e3c07b19 |
78 | |
e132fd56 |
79 | =item * B<dec> |
e3c07b19 |
80 | |
e132fd56 |
81 | =item * B<dec($arg)> |
e3c07b19 |
82 | |
e132fd56 |
83 | Decreases the attribute value by the amount of the argument, or by 1 if no |
84 | argument is given. This method returns the new value. |
7250a968 |
85 | |
e132fd56 |
86 | This method accepts a single argument. |
87 | |
88 | =item * B<reset> |
7250a968 |
89 | |
e132fd56 |
90 | Resets the value stored in this slot to its default value, and returns the new |
91 | value. |
7250a968 |
92 | |
7250a968 |
93 | =back |
94 | |
e3c07b19 |
95 | =head1 BUGS |
96 | |
d4048ef3 |
97 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
98 | |
e3c07b19 |
99 | =cut |