Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Counter; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
bb09ad91 |
5 | our $VERSION = '1.17'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
04e05413 |
9 | use Moose::Meta::Method::Accessor::Native::Counter::dec; |
10 | use Moose::Meta::Method::Accessor::Native::Counter::inc; |
11 | use Moose::Meta::Method::Accessor::Native::Counter::reset; |
12 | use Moose::Meta::Method::Accessor::Native::Counter::set; |
e3c07b19 |
13 | |
04e05413 |
14 | with 'Moose::Meta::Attribute::Native::Trait' => |
15 | { -excludes => ['_root_types'] }; |
e3c07b19 |
16 | |
2edb73d9 |
17 | sub _default_default { 0 } |
18 | sub _default_is { 'ro' } |
2e069f5a |
19 | sub _helper_type { 'Num' } |
04e05413 |
20 | sub _root_types { 'Num', 'Int' } |
e3c07b19 |
21 | |
e3c07b19 |
22 | no Moose::Role; |
23 | |
e3c07b19 |
24 | 1; |
25 | |
26 | __END__ |
27 | |
28 | =pod |
29 | |
30 | =head1 NAME |
31 | |
2420461c |
32 | Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for counters |
e3c07b19 |
33 | |
34 | =head1 SYNOPSIS |
35 | |
36 | package MyHomePage; |
37 | use Moose; |
e3c07b19 |
38 | |
39 | has 'counter' => ( |
e132fd56 |
40 | traits => ['Counter'], |
41 | is => 'ro', |
42 | isa => 'Num', |
43 | default => 0, |
44 | handles => { |
5f3663b2 |
45 | inc_counter => 'inc', |
46 | dec_counter => 'dec', |
47 | reset_counter => 'reset', |
9610c1d2 |
48 | }, |
e3c07b19 |
49 | ); |
50 | |
51 | my $page = MyHomePage->new(); |
e132fd56 |
52 | $page->inc_counter; # same as $page->counter( $page->counter + 1 ); |
53 | $page->dec_counter; # same as $page->counter( $page->counter - 1 ); |
54 | |
aa276b31 |
55 | my $count_by_twos = 2; |
56 | $page->inc_counter($count_by_twos); |
e3c07b19 |
57 | |
58 | =head1 DESCRIPTION |
59 | |
7795e4de |
60 | This trait provides native delegation methods for counters. A counter can be |
61 | any sort of number (integer or not). The delegation methods allow you to |
62 | increment, decrement, or reset the value. |
63 | |
64 | =head1 DEFAULT TYPE |
65 | |
66 | If you don't provide an C<isa> value for your attribute, it will default to |
67 | C<Num>. |
e3c07b19 |
68 | |
e3c07b19 |
69 | =head1 PROVIDED METHODS |
70 | |
e3c07b19 |
71 | =over 4 |
72 | |
e132fd56 |
73 | =item * B<set($value)> |
e3c07b19 |
74 | |
e132fd56 |
75 | Sets the counter to the specified value and returns the new value. |
e3c07b19 |
76 | |
e132fd56 |
77 | This method requires a single argument. |
e3c07b19 |
78 | |
e132fd56 |
79 | =item * B<inc> |
e3c07b19 |
80 | |
e132fd56 |
81 | =item * B<inc($arg)> |
e3c07b19 |
82 | |
e132fd56 |
83 | Increases the attribute value by the amount of the argument, or by 1 if no |
84 | argument is given. This method returns the new value. |
e3c07b19 |
85 | |
e132fd56 |
86 | This method accepts a single argument. |
e3c07b19 |
87 | |
e132fd56 |
88 | =item * B<dec> |
e3c07b19 |
89 | |
e132fd56 |
90 | =item * B<dec($arg)> |
e3c07b19 |
91 | |
e132fd56 |
92 | Decreases the attribute value by the amount of the argument, or by 1 if no |
93 | argument is given. This method returns the new value. |
7250a968 |
94 | |
e132fd56 |
95 | This method accepts a single argument. |
96 | |
97 | =item * B<reset> |
7250a968 |
98 | |
e132fd56 |
99 | Resets the value stored in this slot to its default value, and returns the new |
100 | value. |
7250a968 |
101 | |
7250a968 |
102 | =back |
103 | |
e3c07b19 |
104 | =head1 BUGS |
105 | |
d4048ef3 |
106 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
107 | |
108 | =head1 AUTHOR |
109 | |
110 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
111 | |
112 | =head1 COPYRIGHT AND LICENSE |
113 | |
114 | Copyright 2007-2009 by Infinity Interactive, Inc. |
115 | |
116 | L<http://www.iinteractive.com> |
117 | |
118 | This library is free software; you can redistribute it and/or modify |
119 | it under the same terms as Perl itself. |
120 | |
121 | =cut |