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