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