46f51786c3380c04cf022d27a378fed6468bf45e
[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 $AUTHORITY = 'cpan:STEVAN';
6
7 with 'Moose::Meta::Attribute::Native::Trait' =>
8     { -excludes => ['_root_types'] };
9
10 sub _default_default { 0 }
11 sub _default_is { 'ro' }
12 sub _helper_type { 'Num' }
13 sub _root_types { 'Num', 'Int' }
14
15 no Moose::Role;
16
17 1;
18
19 # ABSTRACT: Helper trait for counters
20
21 __END__
22
23 =pod
24
25 =head1 SYNOPSIS
26
27   package MyHomePage;
28   use Moose;
29
30   has 'counter' => (
31       traits  => ['Counter'],
32       is      => 'ro',
33       isa     => 'Num',
34       default => 0,
35       handles => {
36           inc_counter   => 'inc',
37           dec_counter   => 'dec',
38           reset_counter => 'reset',
39       },
40   );
41
42   my $page = MyHomePage->new();
43   $page->inc_counter;    # same as $page->counter( $page->counter + 1 );
44   $page->dec_counter;    # same as $page->counter( $page->counter - 1 );
45
46   my $count_by_twos = 2;
47   $page->inc_counter($count_by_twos);
48
49 =head1 DESCRIPTION
50
51 This trait provides native delegation methods for counters. A counter can be
52 any sort of number (integer or not). The delegation methods allow you to
53 increment, decrement, or reset the value.
54
55 =head1 DEFAULT TYPE
56
57 If you don't provide an C<isa> value for your attribute, it will default to
58 C<Num>.
59
60 =head1 PROVIDED METHODS
61
62 =over 4
63
64 =item * B<set($value)>
65
66 Sets the counter to the specified value and returns the new value.
67
68 This method requires a single argument.
69
70 =item * B<inc>
71
72 =item * B<inc($arg)>
73
74 Increases the attribute value by the amount of the argument, or by 1 if no
75 argument is given. This method returns the new value.
76
77 This method accepts a single argument.
78
79 =item * B<dec>
80
81 =item * B<dec($arg)>
82
83 Decreases the attribute value by the amount of the argument, or by 1 if no
84 argument is given. This method returns the new value.
85
86 This method accepts a single argument.
87
88 =item * B<reset>
89
90 Resets the value stored in this slot to its default value, and returns the new
91 value.
92
93 =back
94
95 =head1 BUGS
96
97 See L<Moose/BUGS> for details on reporting bugs.
98
99 =cut