fixing this to work correctly
[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
11has '+method_constructors' => (
12 default => sub {
13 return +{
14 inc => sub {
15 my $attr = shift;
16 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
17 },
18 dec => sub {
19 my $attr = shift;
20 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };
21 },
22 }
22d869ff 23 }
d26633fc 24);
22d869ff 25
d26633fc 26sub _process_options_for_provides {
27 my ($self, $options) = @_;
28 (exists $options->{isa})
29 || confess "You must define a type with the Counter metaclass";
30
88aaf2bd 31 (find_type_constraint($options->{isa})->is_a_type_of('Num'))
32 || confess "The type constraint for a Counter ($options->{isa}) must be a subtype of Num";
d26633fc 33}
22d869ff 34
22d869ff 35no Moose;
88aaf2bd 36no Moose::Util::TypeConstraints;
22d869ff 37
38# register the alias ...
39package Moose::Meta::Attribute::Custom::Counter;
40sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
41
421;
43
44__END__
45
46=pod
47
48=head1 NAME
49
50MooseX::AttributeHelpers::Counter
51
52=head1 SYNOPSIS
53
54 package MyHomePage;
55 use Moose;
56
57 has 'counter' => (
58 metaclass => 'Counter',
59 is => 'rw',
60 isa => 'Int',
61 default => sub { 0 },
62 provides => {
63 inc => 'inc_counter',
64 }
65 );
66
67 my $page = MyHomePage->new();
68 $page->inc_counter; # same as $page->counter($page->counter + 1);
69
70=head1 DESCRIPTION
71
72=head1 METHODS
73
74=head1 BUGS
75
76All complex software has bugs lurking in it, and this module is no
77exception. If you find a bug please either email me, or add the bug
78to cpan-RT.
79
80=head1 AUTHOR
81
82Stevan Little E<lt>stevan@iinteractive.comE<gt>
83
84=head1 COPYRIGHT AND LICENSE
85
86Copyright 2007 by Infinity Interactive, Inc.
87
88L<http://www.iinteractive.com>
89
90This library is free software; you can redistribute it and/or modify
91it under the same terms as Perl itself.
92
93=cut