Prep for version 0.12_01 release to match Moose & MOP dev releases.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Counter;
3use Moose;
22d869ff 4
38430345 5our $VERSION = '0.12_01';
6$VERSION = eval $VERSION;
22d869ff 7our $AUTHORITY = 'cpan:STEVAN';
8
8e3fab6d 9use MooseX::AttributeHelpers::MethodProvider::Counter;
d26633fc 10
8e3fab6d 11extends 'MooseX::AttributeHelpers::Base';
8ba40fb0 12
8e3fab6d 13has '+method_provider' => (
14 default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
d26633fc 15);
8e3fab6d 16
17sub helper_type { 'Num' }
38abf787 18
19before 'process_options_for_provides' => sub {
20 my ($self, $options, $name) = @_;
21
22 # Set some default attribute options here unless already defined
9a976497 23 if ((my $type = $self->helper_type) && !exists $options->{isa}){
24 $options->{isa} = $type;
38abf787 25 }
22d869ff 26
829736f9 27 $options->{is} = 'ro' unless exists $options->{is};
28 $options->{default} = 0 unless exists $options->{default};
29};
30
31after 'check_provides_values' => sub {
32 my $self = shift;
33 my $provides = $self->provides;
34
35 unless (scalar keys %$provides) {
36 my $method_constructors = $self->method_constructors;
37 my $attr_name = $self->name;
38
39 foreach my $method (keys %$method_constructors) {
40 $provides->{$method} = ($method . '_' . $attr_name);
41 }
38abf787 42 }
43};
44
22d869ff 45no Moose;
22d869ff 46
47# register the alias ...
0f31cc28 48package # hide me from search.cpan.org
49 Moose::Meta::Attribute::Custom::Counter;
22d869ff 50sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
51
521;
53
54__END__
55
56=pod
57
58=head1 NAME
59
60MooseX::AttributeHelpers::Counter
61
62=head1 SYNOPSIS
63
64 package MyHomePage;
65 use Moose;
5431dff2 66 use MooseX::AttributeHelpers;
22d869ff 67
68 has 'counter' => (
69 metaclass => 'Counter',
38abf787 70 is => 'ro',
71 isa => 'Num',
22d869ff 72 default => sub { 0 },
73 provides => {
74 inc => 'inc_counter',
21c7045b 75 dec => 'dec_counter',
38abf787 76 reset => 'reset_counter',
22d869ff 77 }
78 );
79
80 my $page = MyHomePage->new();
81 $page->inc_counter; # same as $page->counter($page->counter + 1);
21c7045b 82 $page->dec_counter; # same as $page->counter($page->counter - 1);
22d869ff 83
84=head1 DESCRIPTION
85
5431dff2 86This module provides a simple counter attribute, which can be
87incremented and decremeneted.
88
38abf787 89If your attribute definition does not include any of I<is>, I<isa>,
90I<default> or I<provides> but does use the C<Counter> metaclass,
91then this module applies defaults as in the L</SYNOPSIS>
92above. This allows for a very basic counter definition:
93
94 has 'foo' => (metaclass => 'Counter');
95 $obj->inc_foo;
96
22d869ff 97=head1 METHODS
98
5431dff2 99=over 4
100
b91f57af 101=item B<meta>
102
5431dff2 103=item B<method_provider>
104
105=item B<has_method_provider>
106
107=item B<helper_type>
108
38abf787 109=item B<process_options_for_provides>
110
111Run before its superclass method.
112
829736f9 113=item B<check_provides_values>
114
115Run after its superclass method.
116
5431dff2 117=back
118
c91a1347 119=head1 PROVIDED METHODS
120
121It is important to note that all those methods do in place
122modification of the value stored in the attribute.
123
124=over 4
125
eef0ea50 126=item I<set>
127
128Set the counter to the specified value.
129
c91a1347 130=item I<inc>
131
1aa9e3c9 132Increments the value stored in this slot by 1. Providing an argument will
eef0ea50 133cause the counter to be increased by specified amount.
c91a1347 134
135=item I<dec>
136
eef0ea50 137Decrements the value stored in this slot by 1. Providing an argument will
138cause the counter to be increased by specified amount.
c91a1347 139
140=item I<reset>
141
eef0ea50 142Resets the value stored in this slot to it's default value.
c91a1347 143
144=back
145
22d869ff 146=head1 BUGS
147
148All complex software has bugs lurking in it, and this module is no
149exception. If you find a bug please either email me, or add the bug
150to cpan-RT.
151
152=head1 AUTHOR
153
154Stevan Little E<lt>stevan@iinteractive.comE<gt>
155
156=head1 COPYRIGHT AND LICENSE
157
99c62fb8 158Copyright 2007-2008 by Infinity Interactive, Inc.
22d869ff 159
160L<http://www.iinteractive.com>
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself.
164
38abf787 165=cut