Lots of code cleanup in M::AH traits.
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / Counter.pm
CommitLineData
e3c07b19 1
2package Moose::AttributeHelpers::Trait::Counter;
3use Moose::Role;
4
115601b6 5our $VERSION = '0.85';
e3c07b19 6$VERSION = eval $VERSION;
7our $AUTHORITY = 'cpan:STEVAN';
8
9use Moose::AttributeHelpers::MethodProvider::Counter;
10
11with 'Moose::AttributeHelpers::Trait::Base';
12
13has 'method_provider' => (
14 is => 'ro',
15 isa => 'ClassName',
16 predicate => 'has_method_provider',
17 default => 'Moose::AttributeHelpers::MethodProvider::Counter',
18);
19
2edb73d9 20sub _default_default { 0 }
21sub _default_is { 'ro' }
e3c07b19 22
2edb73d9 23sub helper_type { 'Num' }
e3c07b19 24
2edb73d9 25after '_check_handles_values' => sub {
046c8b5e 26 my $self = shift;
5404f169 27 my $handles = $self->handles;
e3c07b19 28
046c8b5e 29 unless ( scalar keys %$handles ) {
e3c07b19 30 my $method_constructors = $self->method_constructors;
31 my $attr_name = $self->name;
32
046c8b5e 33 foreach my $method ( keys %$method_constructors ) {
34 $handles->{ $method . '_' . $attr_name } = $method;
e3c07b19 35 }
40ef30a5 36
e7470555 37 $self->_set_handles($handles);
38 }
e3c07b19 39};
40
41no Moose::Role;
42
43# register the alias ...
44package # hide me from search.cpan.org
45 Moose::Meta::Attribute::Custom::Trait::Counter;
46sub register_implementation { 'Moose::AttributeHelpers::Trait::Counter' }
47
481;
49
50__END__
51
52=pod
53
54=head1 NAME
55
56Moose::AttributeHelpers::Counter
57
58=head1 SYNOPSIS
59
60 package MyHomePage;
61 use Moose;
62 use Moose::AttributeHelpers;
63
64 has 'counter' => (
65 metaclass => 'Counter',
66 is => 'ro',
67 isa => 'Num',
2edb73d9 68 default => 0,
5f3663b2 69 handles => {
70 inc_counter => 'inc',
71 dec_counter => 'dec',
72 reset_counter => 'reset',
e3c07b19 73 }
74 );
75
76 my $page = MyHomePage->new();
2edb73d9 77 $page->inc_counter; # same as $page->counter( $page->counter + 1 );
78 $page->dec_counter; # same as $page->counter( $page->counter - 1 );
e3c07b19 79
80=head1 DESCRIPTION
81
82This module provides a simple counter attribute, which can be
fa73354b 83incremented and decremented.
e3c07b19 84
85If your attribute definition does not include any of I<is>, I<isa>,
5f3663b2 86I<default> or I<handles> but does use the C<Counter> metaclass,
e3c07b19 87then this module applies defaults as in the L</SYNOPSIS>
88above. This allows for a very basic counter definition:
89
90 has 'foo' => (metaclass => 'Counter');
91 $obj->inc_foo;
92
93=head1 METHODS
94
95=over 4
96
97=item B<meta>
98
99=item B<method_provider>
100
101=item B<has_method_provider>
102
103=item B<helper_type>
104
e3c07b19 105=back
106
107=head1 PROVIDED METHODS
108
109It is important to note that all those methods do in place
110modification of the value stored in the attribute.
111
112=over 4
113
114=item I<set>
115
116Set the counter to the specified value.
117
118=item I<inc>
119
120Increments the value stored in this slot by 1. Providing an argument will
121cause the counter to be increased by specified amount.
122
123=item I<dec>
124
125Decrements the value stored in this slot by 1. Providing an argument will
126cause the counter to be increased by specified amount.
127
128=item I<reset>
129
130Resets the value stored in this slot to it's default value.
131
132=back
133
134=head1 BUGS
135
136All complex software has bugs lurking in it, and this module is no
137exception. If you find a bug please either email me, or add the bug
138to cpan-RT.
139
140=head1 AUTHOR
141
142Stevan Little E<lt>stevan@iinteractive.comE<gt>
143
144=head1 COPYRIGHT AND LICENSE
145
146Copyright 2007-2009 by Infinity Interactive, Inc.
147
148L<http://www.iinteractive.com>
149
150This library is free software; you can redistribute it and/or modify
151it under the same terms as Perl itself.
152
153=cut