Catmandu/Fix/rename.pm

filter: Statementstmt 100% 41 / 41 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 98.1% 54 / 55 CC 6 SCAR 12.0
CC
6
Cov
98%
__ANON__
6.0
_build_fixer
1.0
BEGIN
1.0
CRAP 3.3
« Catmandu/Addable.pm ↑ Summary Catmandu/Fix/Condition/is_false.pm »
1 package Catmandu::Fix::rename;
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 Catmandu::Util::Path qw(as_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::Util qw(is_hash_ref is_array_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has path => (fix_arg => 1);
14 1 has search => (fix_arg => 1);
15 1 has replace => (fix_arg => 1);
16  
17 1 with 'Catmandu::Fix::Builder';
18  
19 1 ▶sub _build_fixer {
Subroutine _build_fixer: called CC=1 SCAR=0.0
20 1 my ($self) = @_;
21 1 my $search = $self->search;
22 1 my $replace = $self->replace;
23 1 my $renamer;
24 4 ▶ $renamer = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
25 4 my $data = $_[0];
26  
27 4 ▶ if (is_hash_ref($data)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_hash_ref($data)) { } 2 2 4
elsif (is_array_ref($data)) { } 1 1 2
28 2 for my $old (keys %$data) {
29 2 my $new = $old;
30 2 my $val = $data->{$old};
31 2 ▶ if ($new =~ s/$search/$replace/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new =~ s/$search/$replace/g) 2 0 2
32 2 delete $data->{$old};
33 2 $data->{$new} = $val;
34 }
35 2 $renamer->($val);
36 }
37 }
38 elsif (is_array_ref($data)) {
39 1 $renamer->($_) for @$data;
40 }
41  
42 4 $data;
43 1 };
44  
45 1 as_path($self->path)->updater($renamer);
46 }
47  
48 1 1;
49  
50 __END__
« Catmandu/Addable.pm ↑ Summary Catmandu/Fix/Condition/is_false.pm »