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