blib/lib/Catmandu/Fix/Bind/maybe.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 7 totaltotal 86.3% 57 / 66 CC 5 SCAR 6.6
CC
5
Cov
96%
bind
4.0
value
2.0
BEGIN
1.0
CRAP 1.9
« blib/lib/Catmandu/Validator/Simple.pm ↑ Summary blib/lib/Catmandu/Importer/CSV.pm »
1 package Catmandu::Fix::Bind::maybe;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.0201_02';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Catmandu::Fix::Bind';
12  
13 # Copied from hiratara's Data::Monad::Maybe
14 sub just {
15 19 ▶ my ($self, @values) = @_;
Subroutine just: called CC=1 SCAR=0.0
Pod undocumented
16 19 bless [@values], __PACKAGE__;
17 }
18  
19 sub nothing {
20 2 ▶ my ($self) = @_;
Subroutine nothing: called CC=1 SCAR=0.0
Pod undocumented
21 2 bless \(my $d = undef), __PACKAGE__;
22 }
23  
24 sub is_nothing {
25 31 ▶ my ($self, $mvar) = @_;
Subroutine is_nothing: called CC=1 SCAR=0.0
Pod undocumented
26 31 reftype $mvar ne 'ARRAY';
27 }
28  
29 sub value {
30 20 ▶ my ($self, $mvar) = @_;
Subroutine value: called CC=2 SCAR=6.9
Pod undocumented
31  
32 20 ▶ if ($self->is_nothing($mvar)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_nothing($mvar)) { } 1 19 20
33 1 {};
34 }
35 else {
36 19 $mvar->[0];
37 }
38 }
39  
40 sub unit {
41 11 ▶ my ($self, $data) = @_;
Subroutine unit: called CC=1 SCAR=0.0
Pod undocumented
42 11 $self->just($data);
43 }
44  
45 sub bind {
46 11 ▶ my ($self, $mvar, $func) = @_;
Subroutine bind: called CC=4 SCAR=13.9
Pod undocumented
47  
48 11 ▶ if ($self->is_nothing($mvar)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_nothing($mvar)) 1 10 11
49 1 return $self->nothing;
50 }
51  
52 10 my $res;
53  
54 10 eval {$res = $func->($self->value($mvar));};
55  
56 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
57 0 return $self->nothing;
58 }
59  
60 9 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 8 1 9
61 8 return $self->just($res);
62 }
63 else {
64 1 return $self->nothing;
65 }
66 }
67  
68 sub result {
69 10 ▶ my ($self, $mvar) = @_;
Subroutine result: called CC=1 SCAR=0.0
Pod undocumented
70 10 $self->value($mvar);
71 }
72  
73 1 1;
74  
75 __END__
76  
77 =pod
78  
79 =head1 NAME
80  
81 Catmandu::Fix::Bind::maybe - a binder that skips fixes if one returns undef or dies
82  
83 =head1 SYNOPSIS
84  
85 do maybe()
86 foo()
87 return_undef() # rest will be ignored
88 bar()
89 end
90  
91 =head1 DESCRIPTION
92  
93 The maybe binder computes all the Fix function and ignores fixes that throw exceptions.
94  
95 =head1 SEE ALSO
96  
97 L<Catmandu::Fix::Bind>
98  
99 =cut
« blib/lib/Catmandu/Validator/Simple.pm ↑ Summary blib/lib/Catmandu/Importer/CSV.pm »