All unit tests passing with refactored stuff, documentation updated significantly.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Counter;
3use Moose;
8683383a 4use MooseX::AttributeHelpers::Sugar;
5
6extends 'MooseX::AttributeHelpers::Base';
22d869ff 7
9a976497 8our $VERSION = '0.03';
22d869ff 9our $AUTHORITY = 'cpan:STEVAN';
10
8683383a 11define_attribute_helper (
12 default_options => {
13 is => 'ro',
14 default => 0,
15 },
8ba40fb0 16
8683383a 17 helper_type => 'Num',
18 method_provider => 'MooseX::AttributeHelpers::MethodProvider::Counter',
19 auto_provide => 1,
20 shortcut => 'Counter',
d26633fc 21);
8e3fab6d 22
22d869ff 23no Moose;
8683383a 24no MooseX::AttributeHelpers::Sugar;
22d869ff 25
261;
27
28__END__
29
30=pod
31
32=head1 NAME
33
34MooseX::AttributeHelpers::Counter
35
36=head1 SYNOPSIS
37
38 package MyHomePage;
39 use Moose;
5431dff2 40 use MooseX::AttributeHelpers;
22d869ff 41
42 has 'counter' => (
43 metaclass => 'Counter',
38abf787 44 is => 'ro',
45 isa => 'Num',
22d869ff 46 default => sub { 0 },
47 provides => {
48 inc => 'inc_counter',
21c7045b 49 dec => 'dec_counter',
38abf787 50 reset => 'reset_counter',
22d869ff 51 }
52 );
53
54 my $page = MyHomePage->new();
55 $page->inc_counter; # same as $page->counter($page->counter + 1);
21c7045b 56 $page->dec_counter; # same as $page->counter($page->counter - 1);
22d869ff 57
58=head1 DESCRIPTION
59
720fa35b 60This module provides a simple counter attribute, which can be incremented and
61decremented. It is important to note that all those methods do in place
62modification of the value stored in the attribute.
5431dff2 63
38abf787 64If your attribute definition does not include any of I<is>, I<isa>,
65I<default> or I<provides> but does use the C<Counter> metaclass,
66then this module applies defaults as in the L</SYNOPSIS>
67above. This allows for a very basic counter definition:
68
69 has 'foo' => (metaclass => 'Counter');
70 $obj->inc_foo;
71
c91a1347 72=head1 PROVIDED METHODS
73
720fa35b 74The methods for this metaclass are provided by
75L<MooseX::AttributeHelpers::MethodProvider::Counter>.
c91a1347 76
22d869ff 77=head1 BUGS
78
79All complex software has bugs lurking in it, and this module is no
80exception. If you find a bug please either email me, or add the bug
81to cpan-RT.
82
83=head1 AUTHOR
84
85Stevan Little E<lt>stevan@iinteractive.comE<gt>
86
87=head1 COPYRIGHT AND LICENSE
88
99c62fb8 89Copyright 2007-2008 by Infinity Interactive, Inc.
22d869ff 90
91L<http://www.iinteractive.com>
92
93This library is free software; you can redistribute it and/or modify
94it under the same terms as Perl itself.
95
38abf787 96=cut