2 package MooseX::AttributeHelpers::Counter;
4 use Moose::Util::TypeConstraints;
7 our $AUTHORITY = 'cpan:STEVAN';
9 extends 'MooseX::AttributeHelpers::Base';
11 has '+method_constructors' => (
16 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) + 1) };
20 return sub { $attr->set_value($_[0], $attr->get_value($_[0]) - 1) };
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";
31 my $isa = $options->{isa};
33 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
34 $isa = find_type_constraint($isa);
37 ($isa->is_a_type_of('Num'))
38 || confess "The type constraint for a Counter ($options->{isa}) must be a subtype of Num";
42 no Moose::Util::TypeConstraints;
44 # register the alias ...
45 package Moose::Meta::Attribute::Custom::Counter;
46 sub register_implementation { 'MooseX::AttributeHelpers::Counter' }
56 MooseX::AttributeHelpers::Counter
64 metaclass => 'Counter',
73 my $page = MyHomePage->new();
74 $page->inc_counter; # same as $page->counter($page->counter + 1);
82 All complex software has bugs lurking in it, and this module is no
83 exception. If you find a bug please either email me, or add the bug
88 Stevan Little E<lt>stevan@iinteractive.comE<gt>
90 =head1 COPYRIGHT AND LICENSE
92 Copyright 2007 by Infinity Interactive, Inc.
94 L<http://www.iinteractive.com>
96 This library is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself.