Catmandu/Fix/Bind/hashmap.pm

filter: Statementstmt 94.0% 63 / 67 Branchbran 81.8% 18 / 22 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 89.1% 99 / 111 CC 16 SCAR 17.1
CC
16
Cov
90%
DESTROY
10.0
bind
6.1
add_to_hash
3.0
CRAP 5.5
« Catmandu/Cmd/import.pm ↑ Summary Catmandu/Fix/import.pm »
1 package Catmandu::Fix::Bind::hashmap;
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 qw(:is);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Catmandu::Fix::Bind', 'Catmandu::Fix::Bind::Group';
13  
14 1 has exporter => (fix_opt => 1);
15 1 has store => (fix_opt => 1);
16 11 ▶has uniq => (fix_opt => 1, default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 has count => (fix_opt => 1);
18 1 has join => (fix_opt => 1);
19 1 has extra_args => (fix_opt => 'collect');
20 1 has hash => (is => 'lazy');
21  
22 12 ▶sub _build_hash {
Subroutine _build_hash: called CC=1 SCAR=0.0
23 12 +{};
24 }
25  
26 9 ▶sub add_to_hash {
Subroutine add_to_hash: called CC=3 SCAR=11.0
Pod undocumented
27 9 my ($self, $key, $val) = @_;
28 9 ▶ if ($self->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->count) { } 3 6 9
elsif ($self->uniq) { } 3 3 6
29 3 $self->hash->{$key} += 1;
30 }
31 elsif ($self->uniq) {
32 3 $self->hash->{$key}->{$val} = 1;
33 }
34 else {
35 3 push @{$self->hash->{$key}}, $val;
36 }
37 }
38  
39 18 ▶sub bind {
Subroutine bind: called CC=5 SCAR=18.1
Pod undocumented
40 18 my ($self, $data, $code) = @_;
41  
42 18 $data = $code->($data);
43  
44 18 my $key = $data->{key};
45 18 my $value = $data->{value};
46  
47 18 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 9 9 18
48 9 ▶ if (is_string($key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_string($key)) { } 9 0 9
elsif (is_array_ref($key)) { } 0 0 0
49 9 $self->add_to_hash($key, $value);
50 }
51 elsif (is_array_ref($key)) {
52 0 for (@$key) {
53 0 $self->add_to_hash($_, $value);
54 }
55 }
56 else {
57 0 warn "$key is not a string or array for $value";
58 }
59 }
60  
61 18 $data;
62 }
63  
64 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=10 SCAR=23.1
65 12 my ($self) = @_;
66 12 my $h = $self->hash;
67 12 my $e;
68 12 ▶ my $args = $self->extra_args // {};
Condition50%1/2
$self->extra_args // {}
l!l
120
MC/DC0%0/1
$self->extra_args // {}
$self->extra_args
Truth table50%1/2
A
A= $self->extra_args
Aresult
11
00
69  
70 12 ▶ if ($self->store) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->store) { } 0 12 12
elsif ($self->exporter) { } 3 9 12
71 0 $e = Catmandu->store($self->store, %$args);
72 }
73 elsif ($self->exporter) {
74 3 $e = Catmandu->exporter($self->exporter, %$args);
75 }
76 else {
77 9 $e = Catmandu->exporter('JSON', line_delimited => 1);
78 }
79  
80 12 ▶ my $sorter = $self->count ? sub {$h->{$b} <=> $h->{$a}} : sub {$a cmp $b};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->count ? : 1 11 12
81  
82 12 my $id = 0;
83 12 for (sort $sorter keys %$h) {
84 6 my $v;
85  
86 6 ▶ if ($self->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->count) { } 2 4 6
elsif ($self->uniq) { } 2 2 4
87 2 $v = $h->{$_};
88 }
89 elsif ($self->uniq) {
90 2 $v = [sort keys %{$h->{$_}}];
91 }
92 else {
93 2 $v = $h->{$_};
94 }
95  
96 6 ▶ if (is_array_ref($v) && $self->join) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($v) and $self->join) 2 4 6
Condition100%3/3
is_array_ref($v) and $self->join
!ll&&!rl&&r
222
MC/DC100%2/2
is_array_ref($v) and $self->join
is_array_ref($v) $self->join
Truth table100%3/3
A and B
A= is_array_ref($v)B= $self->join
ABresult
0X0
100
111
97 2 $v = join $self->join, @$v;
98 }
99  
100 6 $e->add({_id => $_, value => $v});
101 }
102  
103 12 $e->commit;
104 }
105  
106 1 1;
107  
108 __END__
« Catmandu/Cmd/import.pm ↑ Summary Catmandu/Fix/import.pm »