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

filter: Statementstmt 94.5% 70 / 74 Branchbran 79.1% 19 / 24 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 3 totaltotal 88.5% 108 / 122 CC 17 SCAR 17.3
CC
17
Cov
90%
DESTROY
10.0
bind
7.3
add_to_hash
3.0
CRAP 5.6
« blib/lib/Catmandu/Interactive.pm ↑ Summary blib/lib/Catmandu/Fix/Bind/list.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.0201_02';
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';
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 12 ▶has flag => (is => 'rw', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
21 1 has hash => (is => 'lazy');
22  
23 sub _build_hash {
24 12 ▶ +{};
Subroutine _build_hash: called CC=1 SCAR=0.0
25 }
26  
27 sub add_to_hash {
28 9 ▶ my ($self, $key, $val) = @_;
Subroutine add_to_hash: called CC=3 SCAR=11.0
Pod undocumented
29 9 ▶ if ($self->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->count) { } 3 6 9
elsif ($self->uniq) { } 3 3 6
30 3 $self->hash->{$key} += 1;
31 }
32 elsif ($self->uniq) {
33 3 $self->hash->{$key}->{$val} = 1;
34 }
35 else {
36 3 push @{$self->hash->{$key}}, $val;
37 }
38 }
39  
40 sub bind {
41 17 ▶ my ($self, $data, $code, $name, $fixer) = @_;
Subroutine bind: called CC=6 SCAR=19.9
Pod undocumented
42  
43 17 ▶ return if $self->flag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->flag 0 17 17
44  
45 17 $data = $fixer->fix($data);
46  
47 17 my $key = $data->{key};
48 17 my $value = $data->{value};
49  
50 17 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 9 8 17
51 9 ▶ if (is_string($key)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_string($key)) { } 9 0 9
elsif (Ref::Util::XS::is_plain_arrayref($key)) { } 0 0 0
52 9 $self->add_to_hash($key, $value);
53 }
54 elsif (is_array_ref($key)) {
55 0 for (@$key) {
56 0 $self->add_to_hash($_, $value);
57 }
58 }
59 else {
60 0 warn "$key is not a string or array for $value";
61 }
62 }
63  
64 17 $self->flag(1);
65  
66 17 $data;
67 }
68  
69 sub result {
70 18 ▶ my ($self, $mvar) = @_;
Subroutine result: called CC=1 SCAR=0.0
Pod undocumented
71  
72 18 $self->flag(0);
73  
74 18 $mvar;
75 }
76  
77 sub DESTROY {
78 12 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=10 SCAR=23.1
79 12 my $h = $self->hash;
80 12 my $e;
81 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
82  
83 12 ▶ if ($self->store) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->store) { } 0 12 12
elsif ($self->exporter) { } 3 9 12
84 0 $e = Catmandu->store($self->store, %$args);
85 }
86 elsif ($self->exporter) {
87 3 $e = Catmandu->exporter($self->exporter, %$args);
88 }
89 else {
90 9 $e = Catmandu->exporter('JSON', line_delimited => 1);
91 }
92  
93 12 ▶ my $sorter = $self->count ? sub {$h->{$b} <=> $h->{$a}} : sub {$a cmp $b};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->count ? : 1 11 12
94  
95 12 my $id = 0;
96 12 for (sort $sorter keys %$h) {
97 6 my $v;
98  
99 6 ▶ if ($self->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->count) { } 2 4 6
elsif ($self->uniq) { } 2 2 4
100 2 $v = $h->{$_};
101 }
102 elsif ($self->uniq) {
103 2 $v = [sort keys %{$h->{$_}}];
104 }
105 else {
106 2 $v = $h->{$_};
107 }
108  
109 6 ▶ if (is_array_ref($v) && $self->join) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($v) and $self->join) 2 4 6
Condition100%3/3
Ref::Util::XS::is_plain_arrayref($v) and $self->join
!ll&&!rl&&r
222
MC/DC100%2/2
Ref::Util::XS::is_plain_arrayref($v) and $self->join
Ref::Util::XS::is_plain_arrayref($v) $self->join
Truth table100%3/3
A and B
A= Ref::Util::XS::is_plain_arrayref($v)B= $self->join
ABresult
0X0
100
111
110 2 $v = join $self->join, @$v;
111 }
112  
113 6 $e->add({_id => $_, value => $v});
114 }
115  
116 12 $e->commit;
117 }
118  
119 1 1;
120  
121 __END__
122  
123 =pod
124  
125 =head1 NAME
126  
127 Catmandu::Fix::Bind::hashmap - a binder to add key/value pairs to an internal hashmap
128  
129 =head1 SYNOPSIS
130  
131 # Find all ISBN in a stream
132 do hashmap(exporter: JSON, join: ',')
133 copy_field(isbn,key)
134 copy_field(_id,value)
135 end
136  
137 # will export to the YAML exporter a hash map containing all isbn occurrences in the stream
138  
139 { "_id": "ISBN1" , "value": "0121,12912,121" }
140 { "_id": "ISBN2" , "value": "102012" }
141  
142 # Count the number of ISBN occurrences in a stream
143 # File: count.fix:
144 do hashmap(count: 1)
145 copy_field(isbn,key)
146 end
147  
148 # Use the Null exporter to suppress the normal output
149 $ cat /tmp/data.json | catmandu convert JSON --fix count.fix to Null
150  
151 =head1 DESCRIPTION
152  
153 The hashmap binder will insert all key/value pairs given to a internal hashmap that can be exported
154 using an Catmandu::Exporter.
155  
156 If the key is an ARRAY, then multiple key/value pairs will be inserted into the hashmap.
157  
158 By default all the values will be added as an array to the hashmap. Every key will have one
159 or more values.
160  
161 =head1 CONFIGURATION
162  
163 =head2 exporter: EXPORTER
164  
165 The name of an exporter to send the results to. Default: JSON Extra parameters can be added:
166  
167 do hashmap(exporter: JSON, file:/tmp/data.json, count: 1)
168 ...
169 end
170  
171 =head2 store: STORE
172  
173 Send the output to a store instead of an exporter. Extra parameters can be added:
174  
175 do hashmap(store: MongoDB, database_name: test, bag: data, count: 1)
176 ...
177 end
178  
179 =head2 uniq: 0|1
180  
181 All the values for the a key will be unique.
182  
183 =head2 join: CHAR
184  
185 Join all the values of a key using a delimiter.
186  
187 =head2 count: 0|1
188  
189 Don't store the values only count the number of key occurrences.
190  
191 =head1 SEE ALSO
192  
193 L<Catmandu::Fix::Bind>
194  
195 =cut
« blib/lib/Catmandu/Interactive.pm ↑ Summary blib/lib/Catmandu/Fix/Bind/list.pm »