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 and not exists $options->{isa}){
23 $options->{isa} = $self->helper_type;
25 $options->{is} = 'ro' unless exists $options->{is};
26 $options->{default} = 0 unless exists $options->{default};
28 # If no provides are specified we'll default to all of them
29 unless ( exists $options->{provides} and
30 grep { exists $options->{provides}{$_} } qw( inc dec reset )
32 @{$options->{provides}}{qw(inc dec reset)} = ("inc_$name", "dec_$name", "reset_$name");
38 # register the alias ...
39 package Moose::Meta::Attribute::Custom::Counter;
40 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
50 MooseX::AttributeHelpers::Counter
56 use MooseX::AttributeHelpers;
59 metaclass => 'Counter',
66 reset => 'reset_counter',
70 my $page = MyHomePage->new();
71 $page->inc_counter; # same as $page->counter($page->counter + 1);
72 $page->dec_counter; # same as $page->counter($page->counter - 1);
76 This module provides a simple counter attribute, which can be
77 incremented and decremeneted.
79 If your attribute definition does not include any of I<is>, I<isa>,
80 I<default> or I<provides> but does use the C<Counter> metaclass,
81 then this module applies defaults as in the L</SYNOPSIS>
82 above. This allows for a very basic counter definition:
84 has 'foo' => (metaclass => 'Counter');
91 =item B<method_provider>
93 =item B<has_method_provider>
97 =item B<process_options_for_provides>
99 Run before its superclass method.
103 =head1 PROVIDED METHODS
105 It is important to note that all those methods do in place
106 modification of the value stored in the attribute.
112 Increments the value stored in this slot by 1.
116 Decrements the value stored in this slot by 1.
120 Resets the value stored in this slot to it's default value.
126 All complex software has bugs lurking in it, and this module is no
127 exception. If you find a bug please either email me, or add the bug
132 Stevan Little E<lt>stevan@iinteractive.comE<gt>
134 =head1 COPYRIGHT AND LICENSE
136 Copyright 2007 by Infinity Interactive, Inc.
138 L<http://www.iinteractive.com>
140 This library is free software; you can redistribute it and/or modify
141 it under the same terms as Perl itself.