2 package MooseX::AttributeHelpers::Counter;
6 our $AUTHORITY = 'cpan:STEVAN';
8 use MooseX::AttributeHelpers::MethodProvider::Counter;
10 extends 'MooseX::AttributeHelpers::Base';
12 has '+method_provider' => (
13 default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
16 sub helper_type { 'Num' }
18 before 'process_options_for_provides' => sub {
19 my ($self, $options, $name) = @_;
21 # Set some default attribute options here unless already defined
22 if ((my $type = $self->helper_type) && !exists $options->{isa}){
23 $options->{isa} = $type;
26 $options->{is} = 'ro' unless exists $options->{is};
27 $options->{default} = 0 unless exists $options->{default};
30 after 'check_provides_values' => sub {
32 my $provides = $self->provides;
34 unless (scalar keys %$provides) {
35 my $method_constructors = $self->method_constructors;
36 my $attr_name = $self->name;
38 foreach my $method (keys %$method_constructors) {
39 $provides->{$method} = ($method . '_' . $attr_name);
46 # register the alias ...
47 package # hide me from search.cpan.org
48 Moose::Meta::Attribute::Custom::Counter;
49 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
59 MooseX::AttributeHelpers::Counter
65 use MooseX::AttributeHelpers;
68 metaclass => 'Counter',
75 reset => 'reset_counter',
79 my $page = MyHomePage->new();
80 $page->inc_counter; # same as $page->counter($page->counter + 1);
81 $page->dec_counter; # same as $page->counter($page->counter - 1);
85 This module provides a simple counter attribute, which can be
86 incremented and decremeneted.
88 If your attribute definition does not include any of I<is>, I<isa>,
89 I<default> or I<provides> but does use the C<Counter> metaclass,
90 then this module applies defaults as in the L</SYNOPSIS>
91 above. This allows for a very basic counter definition:
93 has 'foo' => (metaclass => 'Counter');
102 =item B<method_provider>
104 =item B<has_method_provider>
108 =item B<process_options_for_provides>
110 Run before its superclass method.
112 =item B<check_provides_values>
114 Run after its superclass method.
118 =head1 PROVIDED METHODS
120 It is important to note that all those methods do in place
121 modification of the value stored in the attribute.
127 Set the counter to the specified value.
131 Increments the value stored in this slot by 1.Providing an argument will
132 cause the counter to be increased by specified amount.
136 Decrements the value stored in this slot by 1. Providing an argument will
137 cause the counter to be increased by specified amount.
141 Resets the value stored in this slot to it's default value.
147 All complex software has bugs lurking in it, and this module is no
148 exception. If you find a bug please either email me, or add the bug
153 Stevan Little E<lt>stevan@iinteractive.comE<gt>
155 =head1 COPYRIGHT AND LICENSE
157 Copyright 2007-2008 by Infinity Interactive, Inc.
159 L<http://www.iinteractive.com>
161 This library is free software; you can redistribute it and/or modify
162 it under the same terms as Perl itself.