Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Counter; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
96539d20 |
5 | our $VERSION = '0.87'; |
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 | |
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 | |
e3c07b19 |
42 | 1; |
43 | |
44 | __END__ |
45 | |
46 | =pod |
47 | |
48 | =head1 NAME |
49 | |
c466e58f |
50 | Moose::Meta::Attribute::Native::Trait::Counter |
e3c07b19 |
51 | |
52 | =head1 SYNOPSIS |
53 | |
54 | package MyHomePage; |
55 | use Moose; |
56 | use Moose::AttributeHelpers; |
57 | |
58 | has 'counter' => ( |
59 | metaclass => 'Counter', |
60 | is => 'ro', |
61 | isa => 'Num', |
2edb73d9 |
62 | default => 0, |
5f3663b2 |
63 | handles => { |
64 | inc_counter => 'inc', |
65 | dec_counter => 'dec', |
66 | reset_counter => 'reset', |
e3c07b19 |
67 | } |
68 | ); |
69 | |
70 | my $page = MyHomePage->new(); |
2edb73d9 |
71 | $page->inc_counter; # same as $page->counter( $page->counter + 1 ); |
72 | $page->dec_counter; # same as $page->counter( $page->counter - 1 ); |
e3c07b19 |
73 | |
74 | =head1 DESCRIPTION |
75 | |
76 | This module provides a simple counter attribute, which can be |
fa73354b |
77 | incremented and decremented. |
e3c07b19 |
78 | |
79 | If your attribute definition does not include any of I<is>, I<isa>, |
5f3663b2 |
80 | I<default> or I<handles> but does use the C<Counter> metaclass, |
e3c07b19 |
81 | then this module applies defaults as in the L</SYNOPSIS> |
82 | above. This allows for a very basic counter definition: |
83 | |
84 | has 'foo' => (metaclass => 'Counter'); |
85 | $obj->inc_foo; |
86 | |
87 | =head1 METHODS |
88 | |
89 | =over 4 |
90 | |
91 | =item B<meta> |
92 | |
93 | =item B<method_provider> |
94 | |
95 | =item B<has_method_provider> |
96 | |
e3c07b19 |
97 | =back |
98 | |
99 | =head1 PROVIDED METHODS |
100 | |
101 | It is important to note that all those methods do in place |
102 | modification of the value stored in the attribute. |
103 | |
104 | =over 4 |
105 | |
106 | =item I<set> |
107 | |
108 | Set the counter to the specified value. |
109 | |
110 | =item I<inc> |
111 | |
112 | Increments the value stored in this slot by 1. Providing an argument will |
113 | cause the counter to be increased by specified amount. |
114 | |
115 | =item I<dec> |
116 | |
117 | Decrements 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<reset> |
121 | |
122 | Resets the value stored in this slot to it's default value. |
123 | |
124 | =back |
125 | |
126 | =head1 BUGS |
127 | |
128 | All complex software has bugs lurking in it, and this module is no |
129 | exception. If you find a bug please either email me, or add the bug |
130 | to cpan-RT. |
131 | |
132 | =head1 AUTHOR |
133 | |
134 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
135 | |
136 | =head1 COPYRIGHT AND LICENSE |
137 | |
138 | Copyright 2007-2009 by Infinity Interactive, Inc. |
139 | |
140 | L<http://www.iinteractive.com> |
141 | |
142 | This library is free software; you can redistribute it and/or modify |
143 | it under the same terms as Perl itself. |
144 | |
145 | =cut |