Commit | Line | Data |
e3c07b19 |
1 | |
2 | package Moose::AttributeHelpers::Trait::Counter; |
3 | use Moose::Role; |
4 | |
115601b6 |
5 | our $VERSION = '0.85'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | use Moose::AttributeHelpers::MethodProvider::Counter; |
10 | |
11 | with 'Moose::AttributeHelpers::Trait::Base'; |
12 | |
13 | has 'method_provider' => ( |
14 | is => 'ro', |
15 | isa => 'ClassName', |
16 | predicate => 'has_method_provider', |
17 | default => 'Moose::AttributeHelpers::MethodProvider::Counter', |
18 | ); |
19 | |
2edb73d9 |
20 | sub _default_default { 0 } |
21 | sub _default_is { 'ro' } |
2e069f5a |
22 | sub _helper_type { 'Num' } |
e3c07b19 |
23 | |
2edb73d9 |
24 | after '_check_handles_values' => sub { |
046c8b5e |
25 | my $self = shift; |
5404f169 |
26 | my $handles = $self->handles; |
e3c07b19 |
27 | |
046c8b5e |
28 | unless ( scalar keys %$handles ) { |
e3c07b19 |
29 | my $method_constructors = $self->method_constructors; |
30 | my $attr_name = $self->name; |
31 | |
046c8b5e |
32 | foreach my $method ( keys %$method_constructors ) { |
33 | $handles->{ $method . '_' . $attr_name } = $method; |
e3c07b19 |
34 | } |
40ef30a5 |
35 | |
e7470555 |
36 | $self->_set_handles($handles); |
37 | } |
e3c07b19 |
38 | }; |
39 | |
40 | no Moose::Role; |
41 | |
42 | # register the alias ... |
43 | package # hide me from search.cpan.org |
44 | Moose::Meta::Attribute::Custom::Trait::Counter; |
45 | sub register_implementation { 'Moose::AttributeHelpers::Trait::Counter' } |
46 | |
47 | 1; |
48 | |
49 | __END__ |
50 | |
51 | =pod |
52 | |
53 | =head1 NAME |
54 | |
55 | Moose::AttributeHelpers::Counter |
56 | |
57 | =head1 SYNOPSIS |
58 | |
59 | package MyHomePage; |
60 | use Moose; |
61 | use Moose::AttributeHelpers; |
62 | |
63 | has 'counter' => ( |
64 | metaclass => 'Counter', |
65 | is => 'ro', |
66 | isa => 'Num', |
2edb73d9 |
67 | default => 0, |
5f3663b2 |
68 | handles => { |
69 | inc_counter => 'inc', |
70 | dec_counter => 'dec', |
71 | reset_counter => 'reset', |
e3c07b19 |
72 | } |
73 | ); |
74 | |
75 | my $page = MyHomePage->new(); |
2edb73d9 |
76 | $page->inc_counter; # same as $page->counter( $page->counter + 1 ); |
77 | $page->dec_counter; # same as $page->counter( $page->counter - 1 ); |
e3c07b19 |
78 | |
79 | =head1 DESCRIPTION |
80 | |
81 | This module provides a simple counter attribute, which can be |
fa73354b |
82 | incremented and decremented. |
e3c07b19 |
83 | |
84 | If your attribute definition does not include any of I<is>, I<isa>, |
5f3663b2 |
85 | I<default> or I<handles> but does use the C<Counter> metaclass, |
e3c07b19 |
86 | then this module applies defaults as in the L</SYNOPSIS> |
87 | above. This allows for a very basic counter definition: |
88 | |
89 | has 'foo' => (metaclass => 'Counter'); |
90 | $obj->inc_foo; |
91 | |
92 | =head1 METHODS |
93 | |
94 | =over 4 |
95 | |
96 | =item B<meta> |
97 | |
98 | =item B<method_provider> |
99 | |
100 | =item B<has_method_provider> |
101 | |
e3c07b19 |
102 | =back |
103 | |
104 | =head1 PROVIDED METHODS |
105 | |
106 | It is important to note that all those methods do in place |
107 | modification of the value stored in the attribute. |
108 | |
109 | =over 4 |
110 | |
111 | =item I<set> |
112 | |
113 | Set the counter to the specified value. |
114 | |
115 | =item I<inc> |
116 | |
117 | Increments the value stored in this slot by 1. Providing an argument will |
118 | cause the counter to be increased by specified amount. |
119 | |
120 | =item I<dec> |
121 | |
122 | Decrements the value stored in this slot by 1. Providing an argument will |
123 | cause the counter to be increased by specified amount. |
124 | |
125 | =item I<reset> |
126 | |
127 | Resets the value stored in this slot to it's default value. |
128 | |
129 | =back |
130 | |
131 | =head1 BUGS |
132 | |
133 | All complex software has bugs lurking in it, and this module is no |
134 | exception. If you find a bug please either email me, or add the bug |
135 | to cpan-RT. |
136 | |
137 | =head1 AUTHOR |
138 | |
139 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
140 | |
141 | =head1 COPYRIGHT AND LICENSE |
142 | |
143 | Copyright 2007-2009 by Infinity Interactive, Inc. |
144 | |
145 | L<http://www.iinteractive.com> |
146 | |
147 | This library is free software; you can redistribute it and/or modify |
148 | it under the same terms as Perl itself. |
149 | |
150 | =cut |