Prep for version 0.12_01 release to match Moose & MOP dev releases.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
1
2 package MooseX::AttributeHelpers::Counter;
3 use Moose;
4
5 our $VERSION   = '0.12_01';
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 use MooseX::AttributeHelpers::MethodProvider::Counter;
10
11 extends 'MooseX::AttributeHelpers::Base';
12
13 has '+method_provider' => (
14     default => 'MooseX::AttributeHelpers::MethodProvider::Counter'
15 );
16
17 sub helper_type { 'Num' }
18
19 before 'process_options_for_provides' => sub {
20     my ($self, $options, $name) = @_;
21
22     # Set some default attribute options here unless already defined
23     if ((my $type = $self->helper_type) && !exists $options->{isa}){
24         $options->{isa} = $type;
25     }
26     
27     $options->{is}      = 'ro' unless exists $options->{is};
28     $options->{default} = 0    unless exists $options->{default};
29 };
30
31 after 'check_provides_values' => sub {
32     my $self     = shift;
33     my $provides = $self->provides;
34
35     unless (scalar keys %$provides) {
36         my $method_constructors = $self->method_constructors;
37         my $attr_name           = $self->name;
38         
39         foreach my $method (keys %$method_constructors) {
40             $provides->{$method} = ($method . '_' . $attr_name);
41         }
42     }
43 };
44
45 no Moose;
46
47 # register the alias ...
48 package # hide me from search.cpan.org
49     Moose::Meta::Attribute::Custom::Counter;
50 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
51
52 1;
53
54 __END__
55
56 =pod
57
58 =head1 NAME
59
60 MooseX::AttributeHelpers::Counter
61
62 =head1 SYNOPSIS
63
64   package MyHomePage;
65   use Moose;
66   use MooseX::AttributeHelpers;
67   
68   has 'counter' => (
69       metaclass => 'Counter',
70       is        => 'ro',
71       isa       => 'Num',
72       default   => sub { 0 },
73       provides  => {
74           inc => 'inc_counter',
75           dec => 'dec_counter',          
76           reset => 'reset_counter',
77       }
78   );
79
80   my $page = MyHomePage->new();
81   $page->inc_counter; # same as $page->counter($page->counter + 1);
82   $page->dec_counter; # same as $page->counter($page->counter - 1);  
83   
84 =head1 DESCRIPTION
85
86 This module provides a simple counter attribute, which can be 
87 incremented and decremeneted. 
88
89 If your attribute definition does not include any of I<is>, I<isa>,
90 I<default> or I<provides> but does use the C<Counter> metaclass,
91 then this module applies defaults as in the L</SYNOPSIS>
92 above. This allows for a very basic counter definition:
93
94   has 'foo' => (metaclass => 'Counter');
95   $obj->inc_foo;
96
97 =head1 METHODS
98
99 =over 4
100
101 =item B<meta>
102
103 =item B<method_provider>
104
105 =item B<has_method_provider>
106
107 =item B<helper_type>
108
109 =item B<process_options_for_provides>
110
111 Run before its superclass method.
112
113 =item B<check_provides_values>
114
115 Run after its superclass method.
116
117 =back
118
119 =head1 PROVIDED METHODS
120
121 It is important to note that all those methods do in place
122 modification of the value stored in the attribute.
123
124 =over 4
125
126 =item I<set>
127
128 Set the counter to the specified value.
129
130 =item I<inc>
131
132 Increments the value stored in this slot by 1. Providing an argument will
133 cause the counter to be increased by specified amount.
134
135 =item I<dec>
136
137 Decrements the value stored in this slot by 1. Providing an argument will
138 cause the counter to be increased by specified amount.
139
140 =item I<reset>
141
142 Resets the value stored in this slot to it's default value. 
143
144 =back
145
146 =head1 BUGS
147
148 All complex software has bugs lurking in it, and this module is no 
149 exception. If you find a bug please either email me, or add the bug
150 to cpan-RT.
151
152 =head1 AUTHOR
153
154 Stevan Little E<lt>stevan@iinteractive.comE<gt>
155
156 =head1 COPYRIGHT AND LICENSE
157
158 Copyright 2007-2008 by Infinity Interactive, Inc.
159
160 L<http://www.iinteractive.com>
161
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.
164
165 =cut