Commit | Line | Data |
22d869ff |
1 | |
2 | package MooseX::AttributeHelpers::Counter; |
3 | use Moose; |
8683383a |
4 | use MooseX::AttributeHelpers::Sugar; |
5 | |
6 | extends 'MooseX::AttributeHelpers::Base'; |
22d869ff |
7 | |
9a976497 |
8 | our $VERSION = '0.03'; |
22d869ff |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
10 | |
8683383a |
11 | define_attribute_helper ( |
12 | default_options => { |
13 | is => 'ro', |
14 | default => 0, |
15 | }, |
8ba40fb0 |
16 | |
8683383a |
17 | helper_type => 'Num', |
18 | method_provider => 'MooseX::AttributeHelpers::MethodProvider::Counter', |
19 | auto_provide => 1, |
20 | shortcut => 'Counter', |
d26633fc |
21 | ); |
8e3fab6d |
22 | |
22d869ff |
23 | no Moose; |
8683383a |
24 | no MooseX::AttributeHelpers::Sugar; |
22d869ff |
25 | |
26 | 1; |
27 | |
28 | __END__ |
29 | |
30 | =pod |
31 | |
32 | =head1 NAME |
33 | |
34 | MooseX::AttributeHelpers::Counter |
35 | |
36 | =head1 SYNOPSIS |
37 | |
38 | package MyHomePage; |
39 | use Moose; |
5431dff2 |
40 | use MooseX::AttributeHelpers; |
22d869ff |
41 | |
42 | has 'counter' => ( |
43 | metaclass => 'Counter', |
38abf787 |
44 | is => 'ro', |
45 | isa => 'Num', |
22d869ff |
46 | default => sub { 0 }, |
47 | provides => { |
48 | inc => 'inc_counter', |
21c7045b |
49 | dec => 'dec_counter', |
38abf787 |
50 | reset => 'reset_counter', |
22d869ff |
51 | } |
52 | ); |
53 | |
54 | my $page = MyHomePage->new(); |
55 | $page->inc_counter; # same as $page->counter($page->counter + 1); |
21c7045b |
56 | $page->dec_counter; # same as $page->counter($page->counter - 1); |
22d869ff |
57 | |
58 | =head1 DESCRIPTION |
59 | |
5431dff2 |
60 | This module provides a simple counter attribute, which can be |
61 | incremented and decremeneted. |
62 | |
38abf787 |
63 | If your attribute definition does not include any of I<is>, I<isa>, |
64 | I<default> or I<provides> but does use the C<Counter> metaclass, |
65 | then this module applies defaults as in the L</SYNOPSIS> |
66 | above. This allows for a very basic counter definition: |
67 | |
68 | has 'foo' => (metaclass => 'Counter'); |
69 | $obj->inc_foo; |
70 | |
22d869ff |
71 | =head1 METHODS |
72 | |
5431dff2 |
73 | =over 4 |
74 | |
b91f57af |
75 | =item B<meta> |
76 | |
c91a1347 |
77 | =head1 PROVIDED METHODS |
78 | |
79 | It is important to note that all those methods do in place |
80 | modification of the value stored in the attribute. |
81 | |
82 | =over 4 |
83 | |
84 | =item I<inc> |
85 | |
86 | Increments the value stored in this slot by 1. |
87 | |
88 | =item I<dec> |
89 | |
90 | Decrements the value stored in this slot by 1. |
91 | |
92 | =item I<reset> |
93 | |
94 | Resets the value stored in this slot to it's default value. |
95 | |
96 | =back |
97 | |
22d869ff |
98 | =head1 BUGS |
99 | |
100 | All complex software has bugs lurking in it, and this module is no |
101 | exception. If you find a bug please either email me, or add the bug |
102 | to cpan-RT. |
103 | |
104 | =head1 AUTHOR |
105 | |
106 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
107 | |
108 | =head1 COPYRIGHT AND LICENSE |
109 | |
99c62fb8 |
110 | Copyright 2007-2008 by Infinity Interactive, Inc. |
22d869ff |
111 | |
112 | L<http://www.iinteractive.com> |
113 | |
114 | This library is free software; you can redistribute it and/or modify |
115 | it under the same terms as Perl itself. |
116 | |
38abf787 |
117 | =cut |