File Coverage

blib/lib/Sub/HandlesVia/HandlerLibrary/Number.pm
Criterion Covered Total %
statement 49 49 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 25 25 100.0
pod 8 8 100.0
total 86 87 98.8


line stmt bran cond sub pod time code
1 10     24   751 use 5.008;
  10         36  
2 10     10   76 use strict;
  10         23  
  10         242  
3 10     10   62 use warnings;
  10         35  
  10         691  
4              
5              
6             our $AUTHORITY = 'cpan:TOBYINK';
7             our $VERSION = '0.045';
8              
9             use Sub::HandlesVia::HandlerLibrary;
10 10     10   3794 our @ISA = 'Sub::HandlesVia::HandlerLibrary';
  10         24  
  10         507  
11              
12             use Sub::HandlesVia::Handler qw( handler );
13 10     10   77 use Types::Standard qw( Num Any Item Defined );
  10         33  
  10         104  
14 10     10   957  
  10         27  
  10         68  
15             our @METHODS = qw( set get add sub mul div mod abs cmp eq ne gt lt ge le );
16              
17             my ($me, $type) = @_;
18             if ($type==Num or $type==Defined) {
19 33     33   124 return {
20 33 100 66     191 trust_mutated => 'maybe',
21             value_type => $type,
22 1         100 };
23             }
24             return $me->SUPER::_type_inspector($type);
25             }
26 32         30299  
27             handler
28             name => 'Number:set',
29             args => 1,
30             signature => [Num],
31             template => '« $ARG »',
32             lvalue_template => '$GET = $ARG',
33             usage => '$value',
34             documentation => "Sets the number to a new value.",
35             _examples => sub {
36             my ( $class, $attr, $method ) = @_;
37             return join "",
38             " my \$object = $class\->new( $attr => 4 );\n",
39 1     1   76 " \$object->$method\( 5 );\n",
40 1         7 " say \$object->$attr; ## ==> 5\n",
41             "\n";
42             },
43             }
44              
45             handler
46 35     35 1 230 name => 'Number:get',
47             args => 0,
48             template => '$GET',
49             documentation => "Returns the current value of the number.",
50             _examples => sub {
51             my ( $class, $attr, $method ) = @_;
52             return join "",
53             " my \$object = $class\->new( $attr => 4 );\n",
54             " say \$object->$method; ## ==> 4\n",
55 1     1   66 "\n";
56 1         7 },
57             }
58              
59             handler
60             name => 'Number:add',
61 3     3 1 25 args => 1,
62             signature => [Num],
63             template => '« $GET + $ARG »',
64             usage => '$addend',
65             documentation => "Adds a number to the existing number, updating the attribute.",
66             _examples => sub {
67             my ( $class, $attr, $method ) = @_;
68             return join "",
69             " my \$object = $class\->new( $attr => 4 );\n",
70             " \$object->$method( 5 );\n",
71             " say \$object->$attr; ## ==> 9\n",
72 1     1   63 "\n";
73 1         8 },
74             }
75              
76             handler
77             name => 'Number:sub',
78             args => 1,
79 67     67 1 289 signature => [Num],
80             template => '« $GET - $ARG »',
81             usage => '$subtrahend',
82             documentation => "Subtracts a number from the existing number, updating the attribute.",
83             _examples => sub {
84             my ( $class, $attr, $method ) = @_;
85             return join "",
86             " my \$object = $class\->new( $attr => 9 );\n",
87             " \$object->$method( 6 );\n",
88             " say \$object->$attr; ## ==> 3\n",
89             "\n";
90 1     1   64 },
91 1         6 }
92              
93             handler
94             name => 'Number:mul',
95             args => 1,
96             signature => [Num],
97 67     67 1 310 template => '« $GET * $ARG »',
98             usage => '$factor',
99             documentation => "Multiplies the existing number by a number, updating the attribute.",
100             _examples => sub {
101             my ( $class, $attr, $method ) = @_;
102             return join "",
103             " my \$object = $class\->new( $attr => 2 );\n",
104             " \$object->$method( 5 );\n",
105             " say \$object->$attr; ## ==> 10\n",
106             "\n";
107             },
108 1     1   64 }
109 1         8  
110             handler
111             name => 'Number:div',
112             args => 1,
113             signature => [Num],
114             template => '« $GET / $ARG »',
115 35     35 1 212 usage => '$divisor',
116             documentation => "Divides the existing number by a number, updating the attribute.",
117             _examples => sub {
118             my ( $class, $attr, $method ) = @_;
119             return join "",
120             " my \$object = $class\->new( $attr => 6 );\n",
121             " \$object->$method( 2 );\n",
122             " say \$object->$attr; ## ==> 3\n",
123             "\n";
124             },
125             }
126 1     1   63  
127 1         6 handler
128             name => 'Number:mod',
129             args => 1,
130             signature => [Num],
131             template => '« $GET % $ARG »',
132             usage => '$divisor',
133 67     67 1 310 documentation => "Finds the current number modulo a divisor, updating the attribute.",
134             _examples => sub {
135             my ( $class, $attr, $method ) = @_;
136             return join "",
137             " my \$object = $class\->new( $attr => 5 );\n",
138             " \$object->$method( 2 );\n",
139             " say \$object->$attr; ## ==> 1\n",
140             "\n";
141             },
142             }
143              
144 1     1   62 handler
145 1         7 name => 'Number:abs',
146             args => 0,
147             template => '« abs($GET) »',
148             additional_validation => 'no incoming values',
149             documentation => "Finds the absolute value of the current number, updating the attribute.",
150             _examples => sub {
151 67     67 1 309 my ( $class, $attr, $method ) = @_;
152             return join "",
153             " my \$object = $class\->new( $attr => -5 );\n",
154             " \$object->$method;\n",
155             " say \$object->$attr; ## ==> 5\n",
156             "\n";
157             },
158             }
159              
160             for my $comparison ( qw/ cmp eq ne lt gt le ge / ) {
161 1     1   62 my $op = {
162 1         6 cmp => '<=>',
163             eq => '==',
164             ne => '!=',
165             lt => '<',
166             gt => '>',
167             le => '<=',
168 35     35 1 357 ge => '>=',
169             }->{$comparison};
170              
171             no strict 'refs';
172             *$comparison = sub {
173             handler
174             name => "Number:$comparison",
175             args => 1,
176             signature => [Num],
177             usage => '$num',
178             template => "\$GET $op \$ARG",
179             documentation => "Returns C<< \$object->attr $op \$num >>.",
180             };
181 10     10   37124 }
  10         38  
  10         1323  
182              
183 28     28   146  
184             1;