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