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 | |
d26633fc |
8 | extends 'MooseX::AttributeHelpers::Base'; |
9 | |
8ba40fb0 |
10 | sub helper_type { 'Num' } |
11 | |
d26633fc |
12 | has '+method_constructors' => ( |
13 | default => sub { |
14 | return +{ |
15 | inc => sub { |
16 | my $attr = shift; |
17 | return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) }; |
18 | }, |
19 | dec => sub { |
20 | my $attr = shift; |
21 | return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) }; |
22 | }, |
23 | } |
22d869ff |
24 | } |
d26633fc |
25 | ); |
22d869ff |
26 | |
22d869ff |
27 | no Moose; |
22d869ff |
28 | |
29 | # register the alias ... |
30 | package Moose::Meta::Attribute::Custom::Counter; |
31 | sub register_implementation { 'MooseX::AttributeHelpers::Counter' } |
32 | |
33 | 1; |
34 | |
35 | __END__ |
36 | |
37 | =pod |
38 | |
39 | =head1 NAME |
40 | |
41 | MooseX::AttributeHelpers::Counter |
42 | |
43 | =head1 SYNOPSIS |
44 | |
45 | package MyHomePage; |
46 | use Moose; |
47 | |
48 | has 'counter' => ( |
49 | metaclass => 'Counter', |
50 | is => 'rw', |
51 | isa => 'Int', |
52 | default => sub { 0 }, |
53 | provides => { |
54 | inc => 'inc_counter', |
21c7045b |
55 | dec => 'dec_counter', |
22d869ff |
56 | } |
57 | ); |
58 | |
59 | my $page = MyHomePage->new(); |
60 | $page->inc_counter; # same as $page->counter($page->counter + 1); |
21c7045b |
61 | $page->dec_counter; # same as $page->counter($page->counter - 1); |
22d869ff |
62 | |
63 | =head1 DESCRIPTION |
64 | |
65 | =head1 METHODS |
66 | |
67 | =head1 BUGS |
68 | |
69 | All complex software has bugs lurking in it, and this module is no |
70 | exception. If you find a bug please either email me, or add the bug |
71 | to cpan-RT. |
72 | |
73 | =head1 AUTHOR |
74 | |
75 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
76 | |
77 | =head1 COPYRIGHT AND LICENSE |
78 | |
79 | Copyright 2007 by Infinity Interactive, Inc. |
80 | |
81 | L<http://www.iinteractive.com> |
82 | |
83 | This library is free software; you can redistribute it and/or modify |
84 | it under the same terms as Perl itself. |
85 | |
86 | =cut |