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