2 package Moose::AttributeHelpers::Trait::Counter;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use Moose::AttributeHelpers::MethodProvider::Counter;
11 with 'Moose::AttributeHelpers::Trait::Base';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'Moose::AttributeHelpers::MethodProvider::Counter',
20 sub _default_default { 0 }
21 sub _default_is { 'ro' }
23 sub helper_type { 'Num' }
25 after '_check_handles_values' => sub {
27 my $handles = $self->handles;
29 unless ( scalar keys %$handles ) {
30 my $method_constructors = $self->method_constructors;
31 my $attr_name = $self->name;
33 foreach my $method ( keys %$method_constructors ) {
34 $handles->{ $method . '_' . $attr_name } = $method;
37 $self->_set_handles($handles);
43 # register the alias ...
44 package # hide me from search.cpan.org
45 Moose::Meta::Attribute::Custom::Trait::Counter;
46 sub register_implementation { 'Moose::AttributeHelpers::Trait::Counter' }
56 Moose::AttributeHelpers::Counter
62 use Moose::AttributeHelpers;
65 metaclass => 'Counter',
72 reset_counter => 'reset',
76 my $page = MyHomePage->new();
77 $page->inc_counter; # same as $page->counter( $page->counter + 1 );
78 $page->dec_counter; # same as $page->counter( $page->counter - 1 );
82 This module provides a simple counter attribute, which can be
83 incremented and decremented.
85 If your attribute definition does not include any of I<is>, I<isa>,
86 I<default> or I<handles> but does use the C<Counter> metaclass,
87 then this module applies defaults as in the L</SYNOPSIS>
88 above. This allows for a very basic counter definition:
90 has 'foo' => (metaclass => 'Counter');
99 =item B<method_provider>
101 =item B<has_method_provider>
107 =head1 PROVIDED METHODS
109 It is important to note that all those methods do in place
110 modification of the value stored in the attribute.
116 Set the counter to the specified value.
120 Increments the value stored in this slot by 1. Providing an argument will
121 cause the counter to be increased by specified amount.
125 Decrements the value stored in this slot by 1. Providing an argument will
126 cause the counter to be increased by specified amount.
130 Resets the value stored in this slot to it's default value.
136 All complex software has bugs lurking in it, and this module is no
137 exception. If you find a bug please either email me, or add the bug
142 Stevan Little E<lt>stevan@iinteractive.comE<gt>
144 =head1 COPYRIGHT AND LICENSE
146 Copyright 2007-2009 by Infinity Interactive, Inc.
148 L<http://www.iinteractive.com>
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself.