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