File Coverage

blib/lib/MooseX/AttributeHelpers/Trait/Counter.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 15 15 100.0


line stmt bran cond sub pod time code
1             package MooseX::AttributeHelpers::Trait::Counter;
2 22     22   90 use Moose::Role;
  22         29  
  22         132  
3              
4             our $VERSION = '0.25';
5              
6 22     22   88318 use MooseX::AttributeHelpers::MethodProvider::Counter;
  22         50  
  22         4142  
7              
8             with 'MooseX::AttributeHelpers::Trait::Base';
9              
10             has 'method_provider' => (
11             is => 'ro',
12             isa => 'ClassName',
13             predicate => 'has_method_provider',
14             default => 'MooseX::AttributeHelpers::MethodProvider::Counter',
15             );
16              
17 13     13 1 3664 sub helper_type { 'Num' }
18              
19             before 'process_options_for_provides' => sub {
20             my ($self, $options, $name) = @_;
21              
22             # Set some default attribute options here unless already defined
23             if ((my $type = $self->helper_type) && !exists $options->{isa}){
24             $options->{isa} = $type;
25             }
26              
27             $options->{is} = 'ro' unless exists $options->{is};
28             $options->{default} = 0 unless exists $options->{default};
29             };
30              
31             after 'check_provides_values' => sub {
32             my $self = shift;
33             my $provides = $self->provides;
34              
35             unless (scalar keys %$provides) {
36             my $method_constructors = $self->method_constructors;
37             my $attr_name = $self->name;
38              
39             foreach my $method (keys %$method_constructors) {
40             $provides->{$method} = ($method . '_' . $attr_name);
41             }
42             }
43             };
44              
45 22     22   130 no Moose::Role;
  22         32  
  22         106  
46              
47             1;
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             MooseX::AttributeHelpers::Trait::Counter
58              
59             =head1 VERSION
60              
61             version 0.25
62              
63             =head1 SYNOPSIS
64              
65             package MyHomePage;
66             use Moose;
67             use MooseX::AttributeHelpers;
68            
69             has 'counter' => (
70             metaclass => 'Counter',
71             is => 'ro',
72             isa => 'Num',
73             default => sub { 0 },
74             provides => {
75             inc => 'inc_counter',
76             dec => 'dec_counter',
77             reset => 'reset_counter',
78             }
79             );
80              
81             my $page = MyHomePage->new();
82             $page->inc_counter; # same as $page->counter($page->counter + 1);
83             $page->dec_counter; # same as $page->counter($page->counter - 1);
84              
85             =head1 DESCRIPTION
86              
87             This module provides a simple counter attribute, which can be
88             incremented and decremeneted.
89              
90             If your attribute definition does not include any of I<is>, I<isa>,
91             I<default> or I<provides> but does use the C<Counter> metaclass,
92             then this module applies defaults as in the L</SYNOPSIS>
93             above. This allows for a very basic counter definition:
94              
95             has 'foo' => (metaclass => 'Counter');
96             $obj->inc_foo;
97              
98             =head1 METHODS
99              
100             =over 4
101              
102             =item B<meta>
103              
104             =item B<method_provider>
105              
106             =item B<has_method_provider>
107              
108             =item B<helper_type>
109              
110             =item B<process_options_for_provides>
111              
112             Run before its superclass method.
113              
114             =item B<check_provides_values>
115              
116             Run after its superclass method.
117              
118             =back
119              
120             =head1 PROVIDED METHODS
121              
122             It is important to note that all those methods do in place
123             modification of the value stored in the attribute.
124              
125             =over 4
126              
127             =item I<set>
128              
129             Set the counter to the specified value.
130              
131             =item I<inc>
132              
133             Increments the value stored in this slot by 1. Providing an argument will
134             cause the counter to be increased by specified amount.
135              
136             =item I<dec>
137              
138             Decrements the value stored in this slot by 1. Providing an argument will
139             cause the counter to be increased by specified amount.
140              
141             =item I<reset>
142              
143             Resets the value stored in this slot to it's default value.
144              
145             =back
146              
147             =head1 SUPPORT
148              
149             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-AttributeHelpers>
150             (or L<bug-MooseX-AttributeHelpers@rt.cpan.org|mailto:bug-MooseX-AttributeHelpers@rt.cpan.org>).
151              
152             There is also a mailing list available for users of this distribution, at
153             L<http://lists.perl.org/list/moose.html>.
154              
155             There is also an irc channel available for users of this distribution, at
156             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
157              
158             =head1 AUTHOR
159              
160             Stevan Little <stevan@iinteractive.com>
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is copyright (c) 2007 by Stevan Little and Infinity Interactive, Inc.
165              
166             This is free software; you can redistribute it and/or modify it under
167             the same terms as the Perl 5 programming language system itself.
168              
169             =cut