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 helper_type { 'Num' }
22 before 'process_options_for_handles' => sub {
23 my ($self, $options, $name) = @_;
25 # Set some default attribute options here unless already defined
26 if ((my $type = $self->helper_type) && !exists $options->{isa}){
27 $options->{isa} = $type;
30 $options->{is} = 'ro' unless exists $options->{is};
31 $options->{default} = 0 unless exists $options->{default};
34 after 'check_handles_values' => sub {
36 my $handles = $self->handles;
38 unless (scalar keys %$handles) {
39 my $method_constructors = $self->method_constructors;
40 my $attr_name = $self->name;
42 foreach my $method (keys %$method_constructors) {
43 $handles->{$method . '_' . $attr_name} = $method;
46 $self->_set_handles($handles);
52 # register the alias ...
53 package # hide me from search.cpan.org
54 Moose::Meta::Attribute::Custom::Trait::Counter;
55 sub register_implementation { 'Moose::AttributeHelpers::Trait::Counter' }
65 Moose::AttributeHelpers::Counter
71 use Moose::AttributeHelpers;
74 metaclass => 'Counter',
81 reset_counter => 'reset',
85 my $page = MyHomePage->new();
86 $page->inc_counter; # same as $page->counter($page->counter + 1);
87 $page->dec_counter; # same as $page->counter($page->counter - 1);
91 This module provides a simple counter attribute, which can be
92 incremented and decremented.
94 If your attribute definition does not include any of I<is>, I<isa>,
95 I<default> or I<handles> but does use the C<Counter> metaclass,
96 then this module applies defaults as in the L</SYNOPSIS>
97 above. This allows for a very basic counter definition:
99 has 'foo' => (metaclass => 'Counter');
108 =item B<method_provider>
110 =item B<has_method_provider>
114 =item B<process_options_for_handles>
116 Run before its superclass method.
118 =item B<check_handles_values>
120 Run after its superclass method.
124 =head1 PROVIDED METHODS
126 It is important to note that all those methods do in place
127 modification of the value stored in the attribute.
133 Set the counter to the specified value.
137 Increments the value stored in this slot by 1. Providing an argument will
138 cause the counter to be increased by specified amount.
142 Decrements the value stored in this slot by 1. Providing an argument will
143 cause the counter to be increased by specified amount.
147 Resets the value stored in this slot to it's default value.
153 All complex software has bugs lurking in it, and this module is no
154 exception. If you find a bug please either email me, or add the bug
159 Stevan Little E<lt>stevan@iinteractive.comE<gt>
161 =head1 COPYRIGHT AND LICENSE
163 Copyright 2007-2009 by Infinity Interactive, Inc.
165 L<http://www.iinteractive.com>
167 This library is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself.