File Coverage

blib/lib/Sub/HandlesVia/HandlerLibrary/Counter.pm
Criterion Covered Total %
statement 29 36 80.5
branch 0 4 0.0
condition 0 3 0.0
subroutine 14 16 87.5
pod 4 4 100.0
total 47 63 74.6


line stmt bran cond sub pod time code
1 9     9   684 use 5.008;
  9         35  
2 9     9   53 use strict;
  9         29  
  9         231  
3 9     9   52 use warnings;
  9         22  
  9         611  
4              
5              
6             our $AUTHORITY = 'cpan:TOBYINK';
7             our $VERSION = '0.045';
8              
9             use Sub::HandlesVia::HandlerLibrary;
10 9     9   3439 our @ISA = 'Sub::HandlesVia::HandlerLibrary';
  9         25  
  9         452  
11              
12             use Sub::HandlesVia::Handler qw( handler );
13 9     9   63 use Types::Standard qw( Optional Int Any Item Defined Num );
  9         20  
  9         119  
14 9     9   863  
  9         24  
  9         58  
15             our @METHODS = qw( set inc dec reset );
16              
17             my ($me, $type) = @_;
18             if ($type == Defined) {
19 0     0   0 return {
20 0 0       0 trust_mutated => 'always',
21             };
22 0         0 }
23             if ($type==Num or $type==Int) {
24             return {
25 0 0 0     0 trust_mutated => 'maybe',
26             value_type => $type,
27 0         0 };
28             }
29             return $me->SUPER::_type_inspector($type);
30             }
31 0         0  
32             handler
33             name => 'Counter:set',
34             args => 1,
35             signature => [Int],
36             template => '« $ARG »',
37             usage => '$value',
38             documentation => 'Sets the counter to the given value.',
39             _examples => sub {
40             my ( $class, $attr, $method ) = @_;
41             return join "",
42             " my \$object = $class\->new( $attr => 0 );\n",
43 1     1   85 " \$object->$method\( 5 );\n",
44 1         8 " say \$object->$attr; ## ==> 5\n",
45             "\n";
46             },
47             }
48              
49             handler
50 67     67 1 287 name => 'Counter:inc',
51             min_args => 0,
52             max_args => 1,
53             signature => [Optional[Int]],
54             template => '« $GET + (#ARG ? $ARG : 1) »',
55             lvalue_template => '$GET += (#ARG ? $ARG : 1)',
56             usage => '$amount?',
57             documentation => 'Increments the counter by C<< $amount >>, or by 1 if no value is given.',
58             _examples => sub {
59             my ( $class, $attr, $method ) = @_;
60             return join "",
61             " my \$object = $class\->new( $attr => 0 );\n",
62             " \$object->$method;\n",
63 1     1   65 " \$object->$method;\n",
64 1         8 " say \$object->$attr; ## ==> 2\n",
65             " \$object->$method( 3 );\n",
66             " say \$object->$attr; ## ==> 5\n",
67             "\n";
68             },
69             }
70              
71             handler
72             name => 'Counter:dec',
73 67     67 1 260 min_args => 0,
74             max_args => 1,
75             signature => [Optional[Int]],
76             template => '« $GET - (#ARG ? $ARG : 1) »',
77             lvalue_template => '$GET -= (#ARG ? $ARG : 1)',
78             usage => '$amount?',
79             documentation => 'Decrements the counter by C<< $amount >>, or by 1 if no value is given.',
80             _examples => sub {
81             my ( $class, $attr, $method ) = @_;
82             return join "",
83             " my \$object = $class\->new( $attr => 10 );\n",
84             " \$object->$method;\n",
85             " \$object->$method;\n",
86 1     1   98 " say \$object->$attr; ## ==> 8\n",
87 1         9 " \$object->$method( 5 );\n",
88             " say \$object->$attr; ## ==> 3\n",
89             "\n";
90             },
91             }
92              
93             handler
94             name => 'Counter:reset',
95             args => 0,
96 67     67 1 303 template => '« $DEFAULT »',
97             default_for_reset => sub { 0 },
98             documentation => 'Sets the counter to its default value, or 0 if it has no default.',
99             _examples => sub {
100             my ( $class, $attr, $method ) = @_;
101             return join "",
102             " my \$object = $class\->new( $attr => 10 );\n",
103 0     0   0 " \$object->$method;\n",
104             " say \$object->$attr; ## ==> 0\n",
105             "\n";
106 1     1   62 },
107 1         7 }
108              
109             1;