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