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