2 package MooseX::AttributeHelpers::Counter;
5 our $VERSION = '0.12_01';
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use MooseX::AttributeHelpers::MethodProvider::Counter;
11 extends 'MooseX::AttributeHelpers::Base';
13 has '+method_provider' => (
14 default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
17 sub helper_type { 'Num' }
19 before 'process_options_for_provides' => sub {
20 my ($self, $options, $name) = @_;
22 # Set some default attribute options here unless already defined
23 if ((my $type = $self->helper_type) && !exists $options->{isa}){
24 $options->{isa} = $type;
27 $options->{is} = 'ro' unless exists $options->{is};
28 $options->{default} = 0 unless exists $options->{default};
31 after 'check_provides_values' => sub {
33 my $provides = $self->provides;
35 unless (scalar keys %$provides) {
36 my $method_constructors = $self->method_constructors;
37 my $attr_name = $self->name;
39 foreach my $method (keys %$method_constructors) {
40 $provides->{$method} = ($method . '_' . $attr_name);
47 # register the alias ...
48 package # hide me from search.cpan.org
49 Moose::Meta::Attribute::Custom::Counter;
50 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
60 MooseX::AttributeHelpers::Counter
66 use MooseX::AttributeHelpers;
69 metaclass => 'Counter',
76 reset => 'reset_counter',
80 my $page = MyHomePage->new();
81 $page->inc_counter; # same as $page->counter($page->counter + 1);
82 $page->dec_counter; # same as $page->counter($page->counter - 1);
86 This module provides a simple counter attribute, which can be
87 incremented and decremeneted.
89 If your attribute definition does not include any of I<is>, I<isa>,
90 I<default> or I<provides> but does use the C<Counter> metaclass,
91 then this module applies defaults as in the L</SYNOPSIS>
92 above. This allows for a very basic counter definition:
94 has 'foo' => (metaclass => 'Counter');
103 =item B<method_provider>
105 =item B<has_method_provider>
109 =item B<process_options_for_provides>
111 Run before its superclass method.
113 =item B<check_provides_values>
115 Run after its superclass method.
119 =head1 PROVIDED METHODS
121 It is important to note that all those methods do in place
122 modification of the value stored in the attribute.
128 Set the counter to the specified value.
132 Increments the value stored in this slot by 1. Providing an argument will
133 cause the counter to be increased by specified amount.
137 Decrements the value stored in this slot by 1. Providing an argument will
138 cause the counter to be increased by specified amount.
142 Resets the value stored in this slot to it's default value.
148 All complex software has bugs lurking in it, and this module is no
149 exception. If you find a bug please either email me, or add the bug
154 Stevan Little E<lt>stevan@iinteractive.comE<gt>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2007-2008 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.