Document length and substr, which are in the MethodProvider
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Counter.pm
CommitLineData
e3c07b19 1
c466e58f 2package Moose::Meta::Attribute::Native::Trait::Counter;
e3c07b19 3use Moose::Role;
4
122a129a 5our $VERSION = '0.89';
e3c07b19 6$VERSION = eval $VERSION;
7our $AUTHORITY = 'cpan:STEVAN';
8
c466e58f 9use Moose::Meta::Attribute::Native::MethodProvider::Counter;
e3c07b19 10
c466e58f 11with 'Moose::Meta::Attribute::Native::Trait';
e3c07b19 12
13has 'method_provider' => (
14 is => 'ro',
15 isa => 'ClassName',
16 predicate => 'has_method_provider',
c466e58f 17 default => 'Moose::Meta::Attribute::Native::MethodProvider::Counter',
e3c07b19 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
e3c07b19 421;
43
44__END__
45
46=pod
47
48=head1 NAME
49
c466e58f 50Moose::Meta::Attribute::Native::Trait::Counter
e3c07b19 51
52=head1 SYNOPSIS
53
54 package MyHomePage;
55 use Moose;
e3c07b19 56
57 has 'counter' => (
9610c1d2 58 traits => ['Counter'],
e3c07b19 59 is => 'ro',
60 isa => 'Num',
2edb73d9 61 default => 0,
5f3663b2 62 handles => {
63 inc_counter => 'inc',
64 dec_counter => 'dec',
65 reset_counter => 'reset',
9610c1d2 66 },
e3c07b19 67 );
68
69 my $page = MyHomePage->new();
2edb73d9 70 $page->inc_counter; # same as $page->counter( $page->counter + 1 );
71 $page->dec_counter; # same as $page->counter( $page->counter - 1 );
e3c07b19 72
73=head1 DESCRIPTION
74
75This module provides a simple counter attribute, which can be
fa73354b 76incremented and decremented.
e3c07b19 77
78If your attribute definition does not include any of I<is>, I<isa>,
7250a968 79I<default> or I<handles> but does use the C<Counter> trait,
e3c07b19 80then this module applies defaults as in the L</SYNOPSIS>
81above. This allows for a very basic counter definition:
82
7250a968 83 has 'foo' => (traits => ['Counter']);
e3c07b19 84 $obj->inc_foo;
85
e3c07b19 86=head1 PROVIDED METHODS
87
7250a968 88These methods are implemented in
89L<Moose::Meta::Attribute::Native::MethodProvider::Counter>. It is important to
90note that all those methods do in place modification of the value stored in
91the attribute.
e3c07b19 92
93=over 4
94
157e0475 95=item I<set($value)>
e3c07b19 96
97Set the counter to the specified value.
98
99=item I<inc>
100
101Increments the value stored in this slot by 1. Providing an argument will
102cause the counter to be increased by specified amount.
103
104=item I<dec>
105
106Decrements the value stored in this slot by 1. Providing an argument will
107cause the counter to be increased by specified amount.
108
109=item I<reset>
110
111Resets the value stored in this slot to it's default value.
112
113=back
114
7250a968 115=head1 METHODS
116
117=over 4
118
119=item B<meta>
120
121=item B<method_provider>
122
123=item B<has_method_provider>
124
125=back
126
e3c07b19 127=head1 BUGS
128
129All complex software has bugs lurking in it, and this module is no
130exception. If you find a bug please either email me, or add the bug
131to cpan-RT.
132
133=head1 AUTHOR
134
135Stevan Little E<lt>stevan@iinteractive.comE<gt>
136
137=head1 COPYRIGHT AND LICENSE
138
139Copyright 2007-2009 by Infinity Interactive, Inc.
140
141L<http://www.iinteractive.com>
142
143This library is free software; you can redistribute it and/or modify
144it under the same terms as Perl itself.
145
146=cut