0ab0d717c1336adee4af084f988582e7061f9ac5
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
1
2 package MooseX::AttributeHelpers::Counter;
3 use Moose;
4
5 our $VERSION   = '0.01';
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 extends 'MooseX::AttributeHelpers::Base';
9
10 sub helper_type { 'Num' }
11
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         }
24     }
25 );
26     
27 no Moose;
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',
55           dec => 'dec_counter',          
56       }
57   );
58
59   my $page = MyHomePage->new();
60   $page->inc_counter; # same as $page->counter($page->counter + 1);
61   $page->dec_counter; # same as $page->counter($page->counter - 1);  
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