fixing this to work correctly
[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 has '+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         }
23     }
24 );
25
26 sub _process_options_for_provides {
27     my ($self, $options) = @_;
28     (exists $options->{isa})
29         || confess "You must define a type with the Counter metaclass";  
30      
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";
33 }
34     
35 no Moose;
36 no Moose::Util::TypeConstraints;
37
38 # register the alias ...
39 package Moose::Meta::Attribute::Custom::Counter;
40 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
41
42 1;
43
44 __END__
45
46 =pod
47
48 =head1 NAME
49
50 MooseX::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
76 All complex software has bugs lurking in it, and this module is no 
77 exception. If you find a bug please either email me, or add the bug
78 to cpan-RT.
79
80 =head1 AUTHOR
81
82 Stevan Little E<lt>stevan@iinteractive.comE<gt>
83
84 =head1 COPYRIGHT AND LICENSE
85
86 Copyright 2007 by Infinity Interactive, Inc.
87
88 L<http://www.iinteractive.com>
89
90 This library is free software; you can redistribute it and/or modify
91 it under the same terms as Perl itself.
92
93 =cut