Commit | Line | Data |
22d869ff |
1 | |
2 | package MooseX::AttributeHelpers::Counter; |
3 | use Moose; |
22d869ff |
4 | |
5 | our $VERSION = '0.01'; |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8e3fab6d |
8 | use MooseX::AttributeHelpers::MethodProvider::Counter; |
d26633fc |
9 | |
8e3fab6d |
10 | extends 'MooseX::AttributeHelpers::Base'; |
8ba40fb0 |
11 | |
8e3fab6d |
12 | has '+method_provider' => ( |
13 | default => 'MooseX::AttributeHelpers::MethodProvider::Counter' |
d26633fc |
14 | ); |
8e3fab6d |
15 | |
16 | sub helper_type { 'Num' } |
38abf787 |
17 | |
18 | before '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 |
36 | no Moose; |
22d869ff |
37 | |
38 | # register the alias ... |
39 | package Moose::Meta::Attribute::Custom::Counter; |
40 | sub register_implementation { 'MooseX::AttributeHelpers::Counter' } |
41 | |
42 | 1; |
43 | |
44 | __END__ |
45 | |
46 | =pod |
47 | |
48 | =head1 NAME |
49 | |
50 | MooseX::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 |
76 | This module provides a simple counter attribute, which can be |
77 | incremented and decremeneted. |
78 | |
38abf787 |
79 | If your attribute definition does not include any of I<is>, I<isa>, |
80 | I<default> or I<provides> but does use the C<Counter> metaclass, |
81 | then this module applies defaults as in the L</SYNOPSIS> |
82 | above. 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 | |
99 | Run before its superclass method. |
100 | |
5431dff2 |
101 | =back |
102 | |
c91a1347 |
103 | =head1 PROVIDED METHODS |
104 | |
105 | It is important to note that all those methods do in place |
106 | modification of the value stored in the attribute. |
107 | |
108 | =over 4 |
109 | |
110 | =item I<inc> |
111 | |
112 | Increments the value stored in this slot by 1. |
113 | |
114 | =item I<dec> |
115 | |
116 | Decrements the value stored in this slot by 1. |
117 | |
118 | =item I<reset> |
119 | |
120 | Resets the value stored in this slot to it's default value. |
121 | |
122 | =back |
123 | |
22d869ff |
124 | =head1 BUGS |
125 | |
126 | All complex software has bugs lurking in it, and this module is no |
127 | exception. If you find a bug please either email me, or add the bug |
128 | to cpan-RT. |
129 | |
130 | =head1 AUTHOR |
131 | |
132 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
133 | |
134 | =head1 COPYRIGHT AND LICENSE |
135 | |
136 | Copyright 2007 by Infinity Interactive, Inc. |
137 | |
138 | L<http://www.iinteractive.com> |
139 | |
140 | This library is free software; you can redistribute it and/or modify |
141 | it under the same terms as Perl itself. |
142 | |
38abf787 |
143 | =cut |