* added typed-ness to collections
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Counter;
3use Moose;
22d869ff 4
5our $VERSION = '0.01';
6our $AUTHORITY = 'cpan:STEVAN';
7
d26633fc 8extends 'MooseX::AttributeHelpers::Base';
9
8ba40fb0 10sub helper_type { 'Num' }
11
d26633fc 12has '+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 27no Moose;
22d869ff 28
29# register the alias ...
30package Moose::Meta::Attribute::Custom::Counter;
31sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
32
331;
34
35__END__
36
37=pod
38
39=head1 NAME
40
41MooseX::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
69All complex software has bugs lurking in it, and this module is no
70exception. If you find a bug please either email me, or add the bug
71to cpan-RT.
72
73=head1 AUTHOR
74
75Stevan Little E<lt>stevan@iinteractive.comE<gt>
76
77=head1 COPYRIGHT AND LICENSE
78
79Copyright 2007 by Infinity Interactive, Inc.
80
81L<http://www.iinteractive.com>
82
83This library is free software; you can redistribute it and/or modify
84it under the same terms as Perl itself.
85
86=cut