tweaking
[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           dec => 'dec_counter',          
58       }
59   );
60
61   my $page = MyHomePage->new();
62   $page->inc_counter; # same as $page->counter($page->counter + 1);
63   $page->dec_counter; # same as $page->counter($page->counter - 1);  
64   
65 =head1 DESCRIPTION
66
67 =head1 METHODS
68
69 =head1 BUGS
70
71 All complex software has bugs lurking in it, and this module is no 
72 exception. If you find a bug please either email me, or add the bug
73 to cpan-RT.
74
75 =head1 AUTHOR
76
77 Stevan Little E<lt>stevan@iinteractive.comE<gt>
78
79 =head1 COPYRIGHT AND LICENSE
80
81 Copyright 2007 by Infinity Interactive, Inc.
82
83 L<http://www.iinteractive.com>
84
85 This library is free software; you can redistribute it and/or modify
86 it under the same terms as Perl itself.
87
88 =cut