2 package MooseX::AttributeHelpers::Trait::Counter;
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
9 use MooseX::AttributeHelpers::MethodProvider::Counter;
11 with 'MooseX::AttributeHelpers::Trait::Base';
13 has 'method_provider' => (
16 predicate => 'has_method_provider',
17 default => 'MooseX::AttributeHelpers::MethodProvider::Counter',
20 sub helper_type { 'Num' }
22 before 'process_options_for_provides' => sub {
23 my ($self, $options, $name) = @_;
25 # Set some default attribute options here unless already defined
26 if ((my $type = $self->helper_type) && !exists $options->{isa}){
27 $options->{isa} = $type;
30 $options->{is} = 'ro' unless exists $options->{is};
31 $options->{default} = 0 unless exists $options->{default};
34 after 'check_provides_values' => sub {
36 my $provides = $self->provides;
38 unless (scalar keys %$provides) {
39 my $method_constructors = $self->method_constructors;
40 my $attr_name = $self->name;
42 foreach my $method (keys %$method_constructors) {
43 $provides->{$method} = ($method . '_' . $attr_name);
58 MooseX::AttributeHelpers::Counter
64 use MooseX::AttributeHelpers;
67 metaclass => 'Counter',
74 reset => 'reset_counter',
78 my $page = MyHomePage->new();
79 $page->inc_counter; # same as $page->counter($page->counter + 1);
80 $page->dec_counter; # same as $page->counter($page->counter - 1);
84 This module provides a simple counter attribute, which can be
85 incremented and decremeneted.
87 If your attribute definition does not include any of I<is>, I<isa>,
88 I<default> or I<provides> but does use the C<Counter> metaclass,
89 then this module applies defaults as in the L</SYNOPSIS>
90 above. This allows for a very basic counter definition:
92 has 'foo' => (metaclass => 'Counter');
101 =item B<method_provider>
103 =item B<has_method_provider>
107 =item B<process_options_for_provides>
109 Run before its superclass method.
111 =item B<check_provides_values>
113 Run after its superclass method.
117 =head1 PROVIDED METHODS
119 It is important to note that all those methods do in place
120 modification of the value stored in the attribute.
126 Set the counter to the specified value.
130 Increments the value stored in this slot by 1. Providing an argument will
131 cause the counter to be increased by specified amount.
135 Decrements the value stored in this slot by 1. Providing an argument will
136 cause the counter to be increased by specified amount.
140 Resets the value stored in this slot to it's default value.
146 All complex software has bugs lurking in it, and this module is no
147 exception. If you find a bug please either email me, or add the bug
152 Stevan Little E<lt>stevan@iinteractive.comE<gt>
154 =head1 COPYRIGHT AND LICENSE
156 Copyright 2007-2009 by Infinity Interactive, Inc.
158 L<http://www.iinteractive.com>
160 This library is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself.