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} = ($method . '_' . $attr_name);
50 # register the alias ...
51 package # hide me from search.cpan.org
52 Moose::Meta::Attribute::Custom::Trait::Counter;
53 sub register_implementation { 'Moose::AttributeHelpers::Trait::Counter' }
63 Moose::AttributeHelpers::Counter
69 use Moose::AttributeHelpers;
72 metaclass => 'Counter',
79 reset => 'reset_counter',
83 my $page = MyHomePage->new();
84 $page->inc_counter; # same as $page->counter($page->counter + 1);
85 $page->dec_counter; # same as $page->counter($page->counter - 1);
89 This module provides a simple counter attribute, which can be
90 incremented and decremeneted.
92 If your attribute definition does not include any of I<is>, I<isa>,
93 I<default> or I<provides> but does use the C<Counter> metaclass,
94 then this module applies defaults as in the L</SYNOPSIS>
95 above. This allows for a very basic counter definition:
97 has 'foo' => (metaclass => 'Counter');
106 =item B<method_provider>
108 =item B<has_method_provider>
112 =item B<process_options_for_handles>
114 Run before its superclass method.
116 =item B<check_handles_values>
118 Run after its superclass method.
122 =head1 PROVIDED METHODS
124 It is important to note that all those methods do in place
125 modification of the value stored in the attribute.
131 Set the counter to the specified value.
135 Increments the value stored in this slot by 1. Providing an argument will
136 cause the counter to be increased by specified amount.
140 Decrements the value stored in this slot by 1. Providing an argument will
141 cause the counter to be increased by specified amount.
145 Resets the value stored in this slot to it's default value.
151 All complex software has bugs lurking in it, and this module is no
152 exception. If you find a bug please either email me, or add the bug
157 Stevan Little E<lt>stevan@iinteractive.comE<gt>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright 2007-2009 by Infinity Interactive, Inc.
163 L<http://www.iinteractive.com>
165 This library is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself.