Commit | Line | Data |
7fc7e054 |
1 | |
2 | package MooseX::AttributeHelpers::Trait::Counter; |
3 | use Moose::Role; |
4 | |
cf0d1310 |
5 | our $VERSION = '0.17'; |
6 | $VERSION = eval $VERSION; |
7fc7e054 |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | use MooseX::AttributeHelpers::MethodProvider::Counter; |
10 | |
cf0d1310 |
11 | with 'MooseX::AttributeHelpers::Trait::Base'; |
12 | |
7fc7e054 |
13 | has 'method_provider' => ( |
14 | is => 'ro', |
15 | isa => 'ClassName', |
16 | predicate => 'has_method_provider', |
17 | default => 'MooseX::AttributeHelpers::MethodProvider::Counter', |
18 | ); |
19 | |
20 | sub helper_type { 'Num' } |
21 | |
22 | before '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 | |
34 | after '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 | |
48 | no Moose::Role; |
49 | |
50 | # register the alias ... |
51 | package # hide me from search.cpan.org |
52 | Moose::Meta::Attribute::Custom::Trait::Counter; |
53 | sub register_implementation { 'MooseX::AttributeHelpers::Trait::Counter' } |
54 | |
55 | 1; |
56 | |
cf0d1310 |
57 | __END__ |
58 | |
59 | =pod |
60 | |
61 | =head1 NAME |
62 | |
63 | MooseX::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 | |
89 | This module provides a simple counter attribute, which can be |
90 | incremented and decremeneted. |
91 | |
92 | If your attribute definition does not include any of I<is>, I<isa>, |
93 | I<default> or I<provides> but does use the C<Counter> metaclass, |
94 | then this module applies defaults as in the L</SYNOPSIS> |
95 | above. 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 | |
114 | Run before its superclass method. |
115 | |
116 | =item B<check_provides_values> |
117 | |
118 | Run after its superclass method. |
119 | |
120 | =back |
121 | |
122 | =head1 PROVIDED METHODS |
123 | |
124 | It is important to note that all those methods do in place |
125 | modification of the value stored in the attribute. |
126 | |
127 | =over 4 |
128 | |
129 | =item I<set> |
130 | |
131 | Set the counter to the specified value. |
132 | |
133 | =item I<inc> |
134 | |
135 | Increments the value stored in this slot by 1. Providing an argument will |
136 | cause the counter to be increased by specified amount. |
137 | |
138 | =item I<dec> |
139 | |
140 | Decrements the value stored in this slot by 1. Providing an argument will |
141 | cause the counter to be increased by specified amount. |
142 | |
143 | =item I<reset> |
144 | |
145 | Resets the value stored in this slot to it's default value. |
146 | |
147 | =back |
148 | |
149 | =head1 BUGS |
150 | |
151 | All complex software has bugs lurking in it, and this module is no |
152 | exception. If you find a bug please either email me, or add the bug |
153 | to cpan-RT. |
154 | |
155 | =head1 AUTHOR |
156 | |
157 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
158 | |
159 | =head1 COPYRIGHT AND LICENSE |
160 | |
9c5d164e |
161 | Copyright 2007-2009 by Infinity Interactive, Inc. |
cf0d1310 |
162 | |
163 | L<http://www.iinteractive.com> |
164 | |
165 | This library is free software; you can redistribute it and/or modify |
166 | it under the same terms as Perl itself. |
167 | |
168 | =cut |