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