Valiant/Filter/Numberize.pm

filter: Statementstmt 100% 17 / 17 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 88.4% 23 / 26 CC 2 SCAR 2.9
CC
2
Cov
95%
filter_each
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« Valiant/JSON/Util.pm ↑ Summary Valiant/Filter.pm »
1 package Valiant::Filter::Numberize;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Valiant::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 with 'Valiant::Filter::Each';
7  
8 1 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=1 SCAR=0.0
Pod undocumented
9 1 my ($class, $arg) = @_;
10 1 return +{ };
11 }
12  
13 3 ▶sub filter_each {
Subroutine filter_each: called CC=2 SCAR=7.0
Pod undocumented
14 3 my ($self, $class, $attrs, $attribute_name) = @_;
15 3 my $value = $attrs->{$attribute_name};
16 3 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
17  
18 1 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 return 0+$value;
20 }
21  
22 1 1;
23  
24 =head1 NAME
25  
26 Valiant::Filter::Numberize - Force into number context
27  
28 =head1 SYNOPSIS
29  
30 package Local::Test::User;
31  
32 use Moo;
33 use Valiant::Filters;
34  
35 has 'age' => (is=>'ro', required=>1);
36  
37 filters age => (
38 numberize => 1,
39 );
40  
41 my $user = Local::Test::User->new(age=>'25');
42 print $user->age; # 25
43  
44 my $not_a_number = Local::Test::User(age=>'aaaabbbccc');
45 print $not_a_number; # 0
46  
47
48 =head1 DESCRIPTION
49  
50 This is a very simple filter that takes no paramters and just forces a value into
51 number context. Please note that this mean strings with letters and other non number
52 values will almost always end up as 0 without returning any validation errors. If you
53 want to notice and handle this as a validation then you might want to check L<Valiant::Validator::Numericality>
54 instead,
55  
56 =head1 SEE ALSO
57
58 L<Valiant>, L<Valiant::Filter>, L<Valiant::Validator::Filter>.
59  
60 =head1 AUTHOR
61
62 See L<Valiant>
63
64 =head1 COPYRIGHT & LICENSE
65
66 See L<Valiant>
67  
68 =cut
« Valiant/JSON/Util.pm ↑ Summary Valiant/Filter.pm »