tweaking
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Counter.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers::Counter;
3use Moose;
88aaf2bd 4use Moose::Util::TypeConstraints;
22d869ff 5
6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:STEVAN';
8
d26633fc 9extends 'MooseX::AttributeHelpers::Base';
10
8ba40fb0 11sub helper_type { 'Num' }
12
d26633fc 13has '+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 }
22d869ff 25 }
d26633fc 26);
22d869ff 27
22d869ff 28no Moose;
88aaf2bd 29no Moose::Util::TypeConstraints;
22d869ff 30
31# register the alias ...
32package Moose::Meta::Attribute::Custom::Counter;
33sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
34
351;
36
37__END__
38
39=pod
40
41=head1 NAME
42
43MooseX::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',
21c7045b 57 dec => 'dec_counter',
22d869ff 58 }
59 );
60
61 my $page = MyHomePage->new();
62 $page->inc_counter; # same as $page->counter($page->counter + 1);
21c7045b 63 $page->dec_counter; # same as $page->counter($page->counter - 1);
22d869ff 64
65=head1 DESCRIPTION
66
67=head1 METHODS
68
69=head1 BUGS
70
71All complex software has bugs lurking in it, and this module is no
72exception. If you find a bug please either email me, or add the bug
73to cpan-RT.
74
75=head1 AUTHOR
76
77Stevan Little E<lt>stevan@iinteractive.comE<gt>
78
79=head1 COPYRIGHT AND LICENSE
80
81Copyright 2007 by Infinity Interactive, Inc.
82
83L<http://www.iinteractive.com>
84
85This library is free software; you can redistribute it and/or modify
86it under the same terms as Perl itself.
87
88=cut