Version 0.96.
[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.96';
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 no Moose::Role;
25
26 1;
27
28 __END__
29
30 =pod
31
32 =head1 NAME
33
34 Moose::Meta::Attribute::Native::Trait::Counter - Helper trait for counters
35
36 =head1 SYNOPSIS
37
38   package MyHomePage;
39   use Moose;
40
41   has 'counter' => (
42       traits    => ['Counter'],
43       is        => 'ro',
44       isa       => 'Num',
45       default   => 0,
46       handles   => {
47           inc_counter   => 'inc',
48           dec_counter   => 'dec',
49           reset_counter => 'reset',
50       },
51   );
52
53   my $page = MyHomePage->new();
54   $page->inc_counter; # same as $page->counter( $page->counter + 1 );
55   $page->dec_counter; # same as $page->counter( $page->counter - 1 );
56
57 =head1 DESCRIPTION
58
59 This module provides a simple counter attribute, which can be
60 incremented and decremented.
61
62 If your attribute definition does not include any of I<is>, I<isa>,
63 I<default> or I<handles> but does use the C<Counter> trait,
64 then this module applies defaults as in the L</SYNOPSIS>
65 above. This allows for a very basic counter definition:
66
67   has 'foo' => (traits => ['Counter']);
68   $obj->inc_foo;
69
70 =head1 PROVIDED METHODS
71
72 These methods are implemented in
73 L<Moose::Meta::Attribute::Native::MethodProvider::Counter>. It is important to
74 note that all those methods do in place modification of the value stored in
75 the attribute.
76
77 =over 4
78
79 =item B<set($value)>
80
81 Set the counter to the specified value.
82
83 =item B<inc>
84
85 Increments the value stored in this slot by 1. Providing an argument will
86 cause the counter to be increased by specified amount.
87
88 =item B<dec>
89
90 Decrements the value stored in this slot by 1. Providing an argument will
91 cause the counter to be increased by specified amount.
92
93 =item B<reset>
94
95 Resets the value stored in this slot to it's default value.
96
97 =back
98
99 =head1 METHODS
100
101 =over 4
102
103 =item B<meta>
104
105 =item B<method_provider>
106
107 =item B<has_method_provider>
108
109 =back
110
111 =head1 BUGS
112
113 See L<Moose/BUGS> for details on reporting bugs.
114
115 =head1 AUTHOR
116
117 Stevan Little E<lt>stevan@iinteractive.comE<gt>
118
119 =head1 COPYRIGHT AND LICENSE
120
121 Copyright 2007-2009 by Infinity Interactive, Inc.
122
123 L<http://www.iinteractive.com>
124
125 This library is free software; you can redistribute it and/or modify
126 it under the same terms as Perl itself.
127
128 =cut