Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Counter; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
45160c4b |
5 | our $VERSION = '0.99'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
c466e58f |
9 | use Moose::Meta::Attribute::Native::MethodProvider::Counter; |
e3c07b19 |
10 | |
c466e58f |
11 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
12 | |
13 | has 'method_provider' => ( |
14 | is => 'ro', |
15 | isa => 'ClassName', |
16 | predicate => 'has_method_provider', |
c466e58f |
17 | default => 'Moose::Meta::Attribute::Native::MethodProvider::Counter', |
e3c07b19 |
18 | ); |
19 | |
2edb73d9 |
20 | sub _default_default { 0 } |
21 | sub _default_is { 'ro' } |
2e069f5a |
22 | sub _helper_type { 'Num' } |
e3c07b19 |
23 | |
e3c07b19 |
24 | no Moose::Role; |
25 | |
e3c07b19 |
26 | 1; |
27 | |
28 | __END__ |
29 | |
30 | =pod |
31 | |
32 | =head1 NAME |
33 | |
2420461c |
34 | Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for counters |
e3c07b19 |
35 | |
36 | =head1 SYNOPSIS |
37 | |
38 | package MyHomePage; |
39 | use Moose; |
e3c07b19 |
40 | |
41 | has 'counter' => ( |
9610c1d2 |
42 | traits => ['Counter'], |
e3c07b19 |
43 | is => 'ro', |
44 | isa => 'Num', |
2edb73d9 |
45 | default => 0, |
5f3663b2 |
46 | handles => { |
47 | inc_counter => 'inc', |
48 | dec_counter => 'dec', |
49 | reset_counter => 'reset', |
9610c1d2 |
50 | }, |
e3c07b19 |
51 | ); |
52 | |
53 | my $page = MyHomePage->new(); |
2edb73d9 |
54 | $page->inc_counter; # same as $page->counter( $page->counter + 1 ); |
55 | $page->dec_counter; # same as $page->counter( $page->counter - 1 ); |
aa276b31 |
56 | |
57 | my $count_by_twos = 2; |
58 | $page->inc_counter($count_by_twos); |
e3c07b19 |
59 | |
60 | =head1 DESCRIPTION |
61 | |
62 | This module provides a simple counter attribute, which can be |
aa276b31 |
63 | incremented and decremented by arbitrary amounts. The default |
64 | amount of change is one. |
e3c07b19 |
65 | |
e3c07b19 |
66 | =head1 PROVIDED METHODS |
67 | |
7250a968 |
68 | These methods are implemented in |
69 | L<Moose::Meta::Attribute::Native::MethodProvider::Counter>. It is important to |
70 | note that all those methods do in place modification of the value stored in |
71 | the attribute. |
e3c07b19 |
72 | |
73 | =over 4 |
74 | |
eb465b32 |
75 | =item B<set($value)> |
e3c07b19 |
76 | |
77 | Set the counter to the specified value. |
78 | |
aa276b31 |
79 | =item B<inc($arg)> |
e3c07b19 |
80 | |
aa276b31 |
81 | Increase the attribute value by the amount of the argument. |
82 | No argument increments the value by 1. |
e3c07b19 |
83 | |
aa276b31 |
84 | =item B<dec($arg)> |
e3c07b19 |
85 | |
aa276b31 |
86 | Decrease the attribute value by the amount of the argument. |
87 | No argument decrements the value by 1. |
e3c07b19 |
88 | |
eb465b32 |
89 | =item B<reset> |
e3c07b19 |
90 | |
91 | Resets the value stored in this slot to it's default value. |
92 | |
93 | =back |
94 | |
7250a968 |
95 | =head1 METHODS |
96 | |
97 | =over 4 |
98 | |
99 | =item B<meta> |
100 | |
101 | =item B<method_provider> |
102 | |
103 | =item B<has_method_provider> |
104 | |
105 | =back |
106 | |
e3c07b19 |
107 | =head1 BUGS |
108 | |
d4048ef3 |
109 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
110 | |
111 | =head1 AUTHOR |
112 | |
113 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
114 | |
115 | =head1 COPYRIGHT AND LICENSE |
116 | |
117 | Copyright 2007-2009 by Infinity Interactive, Inc. |
118 | |
119 | L<http://www.iinteractive.com> |
120 | |
121 | This library is free software; you can redistribute it and/or modify |
122 | it under the same terms as Perl itself. |
123 | |
124 | =cut |