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 _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' => ( |
9610c1d2 |
40 | traits => ['Counter'], |
e3c07b19 |
41 | is => 'ro', |
42 | isa => 'Num', |
2edb73d9 |
43 | default => 0, |
5f3663b2 |
44 | handles => { |
45 | inc_counter => 'inc', |
46 | dec_counter => 'dec', |
47 | reset_counter => 'reset', |
9610c1d2 |
48 | }, |
e3c07b19 |
49 | ); |
50 | |
51 | my $page = MyHomePage->new(); |
2edb73d9 |
52 | $page->inc_counter; # same as $page->counter( $page->counter + 1 ); |
53 | $page->dec_counter; # same as $page->counter( $page->counter - 1 ); |
aa276b31 |
54 | |
55 | my $count_by_twos = 2; |
56 | $page->inc_counter($count_by_twos); |
e3c07b19 |
57 | |
58 | =head1 DESCRIPTION |
59 | |
60 | This module provides a simple counter attribute, which can be |
aa276b31 |
61 | incremented and decremented by arbitrary amounts. The default |
62 | amount of change is one. |
e3c07b19 |
63 | |
e3c07b19 |
64 | =head1 PROVIDED METHODS |
65 | |
e3c07b19 |
66 | =over 4 |
67 | |
eb465b32 |
68 | =item B<set($value)> |
e3c07b19 |
69 | |
70 | Set the counter to the specified value. |
71 | |
aa276b31 |
72 | =item B<inc($arg)> |
e3c07b19 |
73 | |
aa276b31 |
74 | Increase the attribute value by the amount of the argument. |
75 | No argument increments the value by 1. |
e3c07b19 |
76 | |
aa276b31 |
77 | =item B<dec($arg)> |
e3c07b19 |
78 | |
aa276b31 |
79 | Decrease the attribute value by the amount of the argument. |
80 | No argument decrements the value by 1. |
e3c07b19 |
81 | |
eb465b32 |
82 | =item B<reset> |
e3c07b19 |
83 | |
84 | Resets the value stored in this slot to it's default value. |
85 | |
86 | =back |
87 | |
7250a968 |
88 | =head1 METHODS |
89 | |
90 | =over 4 |
91 | |
92 | =item B<meta> |
93 | |
7250a968 |
94 | =back |
95 | |
e3c07b19 |
96 | =head1 BUGS |
97 | |
d4048ef3 |
98 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
99 | |
100 | =head1 AUTHOR |
101 | |
102 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
103 | |
104 | =head1 COPYRIGHT AND LICENSE |
105 | |
106 | Copyright 2007-2009 by Infinity Interactive, Inc. |
107 | |
108 | L<http://www.iinteractive.com> |
109 | |
110 | This library is free software; you can redistribute it and/or modify |
111 | it under the same terms as Perl itself. |
112 | |
113 | =cut |