86c13179c4f732539fb072a202292e9f84b66bdf
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
1
2 package MooseX::AttributeHelpers::Counter;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 extends 'MooseX::AttributeHelpers::Base';
10
11 sub helper_type { 'Num' }
12
13 has '+method_constructors' => (
14     default => sub {
15         return +{
16             inc => sub {
17                 my $attr = shift;
18                 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
19             },
20             dec => sub {
21                 my $attr = shift;
22                 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };        
23             },
24         }
25     }
26 );
27     
28 no Moose;
29 no Moose::Util::TypeConstraints;
30
31 # register the alias ...
32 package Moose::Meta::Attribute::Custom::Counter;
33 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
34
35 1;
36
37 __END__
38
39 =pod
40
41 =head1 NAME
42
43 MooseX::AttributeHelpers::Counter
44
45 =head1 SYNOPSIS
46
47   package MyHomePage;
48   use Moose;
49   
50   has 'counter' => (
51       metaclass => 'Counter',
52       is        => 'rw',
53       isa       => 'Int',
54       default   => sub { 0 },
55       provides  => {
56           inc => 'inc_counter',
57       }
58   );
59
60   my $page = MyHomePage->new();
61   $page->inc_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