Catmandu/Fix/Bind/visitor.pm

filter: Statementstmt 85.1% 46 / 54 Branchbran 63.6% 14 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 5 totaltotal 75.5% 68 / 90 CC 13 SCAR 14.4
CC
13
Cov
79%
bind_hash
7.1
bind_array
4.3
bind
3.8
CRAP 4.2
« Catmandu/Exporter/Text.pm ↑ Summary Catmandu/Plugin/Versioning.pm »
1 package Catmandu::Fix::Bind::visitor;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.2025';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Util;
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', 'Catmandu::Fix::Bind::Group';
12  
13 1 has path => (is => 'ro');
14  
15 18 ▶sub unit {
Subroutine unit: called CC=2 SCAR=7.6
Pod undocumented
16 18 my ($self, $data) = @_;
17  
18 18 ▶ if (defined $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path) { } 0 18 18
19 0 return Catmandu::Util::data_at($self->path, $data);
20 }
21 else {
22 18 return $data;
23 }
24 }
25  
26 18 ▶sub bind {
Subroutine bind: called CC=3 SCAR=13.3
Pod undocumented
27 18 my ($self, $mvar, $func) = @_;
28  
29 18 ▶ if (Catmandu::Util::is_array_ref($mvar)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($mvar)) { } 0 18 18
elsif (Catmandu::Util::is_hash_ref($mvar)) { } 18 0 18
30 0 return $self->bind_array($mvar, $func, '');
31 }
32 elsif (Catmandu::Util::is_hash_ref($mvar)) {
33 18 return $self->bind_hash($mvar, $func, '');
34 }
35 else {
36 0 return $self->bind_hash($mvar, $func, '');
37 }
38 }
39  
40 0 ▶sub bind_scalar {
Subroutine bind_scalar: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my ($self, $mvar, $func, $parent) = @_;
42  
43 0 return $func->({'key' => $parent, 'scalar' => $mvar})->{'scalar'};
44 }
45  
46 1 ▶sub bind_array {
Subroutine bind_array: called CC=4 SCAR=14.7
Pod undocumented
47 1 my ($self, $mvar, $func, $parent) = @_;
48  
49 1 my $result = $func->({'key' => $parent, 'array' => $mvar});
50  
51 1 my $new_var = [];
52  
53 1 ▶ for (my $i = 0; $i < @$mvar; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$mvar) 2 1 3
54 2 my $item = $mvar->[$i];
55 2 ▶ if (Catmandu::Util::is_array_ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($item)) { } 0 2 2
elsif (Catmandu::Util::is_hash_ref($item)) { } 2 0 2
56 0 my ($newkey, $newvalue) = $self->bind_array($item, $func, $i);
57 0 $mvar->[$i] = $newvalue;
58 }
59 elsif (Catmandu::Util::is_hash_ref($item)) {
60 2 my ($newkey, $newvalue) = $self->bind_hash($item, $func, $i);
61 2 $mvar->[$i] = $newvalue;
62 }
63 else {
64 $mvar->[$i]
65 0 = $func->({'key' => $i, 'scalar' => $item})->{'scalar'};
66 }
67 }
68  
69 1 return ($result->{key}, $result->{array});
70 }
71  
72 25 ▶sub bind_hash {
Subroutine bind_hash: called CC=7 SCAR=19.6
Pod undocumented
73 25 my ($self, $mvar, $func, $parent) = @_;
74  
75 25 my $result = $func->({'key' => $parent, 'hash' => $mvar});
76  
77 25 for my $key (keys %$mvar) {
78 53 my $item = $mvar->{$key};
79  
80 53 ▶ if (Catmandu::Util::is_array_ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($item)) { } 1 52 53
elsif (Catmandu::Util::is_hash_ref($item)) { } 5 47 52
81  
82 # Keys can update themselves
83 1 my ($newkey, $newvalue) = $self->bind_array($item, $func, $key);
84 1 $mvar->{$newkey} = $newvalue;
85 1 ▶ delete $mvar->{$key} if ($newkey ne $key);
Branch: 1 branch
BranchTrueFalseTotal
if $newkey ne $key 0 1 1
86 }
87 elsif (Catmandu::Util::is_hash_ref($item)) {
88  
89 # Keys can update themselves
90 5 my ($newkey, $newvalue) = $self->bind_hash($item, $func, $key);
91 5 $mvar->{$newkey} = $newvalue;
92 5 ▶ delete $mvar->{$key} if ($newkey ne $key);
Branch: 1 branch
BranchTrueFalseTotal
if $newkey ne $key 0 5 5
93 }
94 else {
95 # Keys can update themselves
96 47 my $result = $func->({'key' => $key, 'scalar' => $item});
97 47 $mvar->{$result->{'key'}} = $result->{'scalar'};
98 47 ▶ delete $mvar->{$key} if ($result->{'key'} ne $key);
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"key"} ne $key 0 47 47
99 }
100 }
101  
102 25 return ($result->{key}, $result->{hash});
103 }
104  
105 1 1;
106  
107 __END__
« Catmandu/Exporter/Text.pm ↑ Summary Catmandu/Plugin/Versioning.pm »