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