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