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