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

filter: Statementstmt 84.7% 39 / 46 Branchbran 68.7% 11 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 5 totaltotal 76.3% 58 / 76 CC 10 SCAR 11.1
CC
10
Cov
81%
bind_array
4.2
bind_hash
4.0
bind
3.8
CRAP 3.0
« blib/lib/Catmandu/Fix/Bind/with.pm ↑ Summary blib/lib/Catmandu/Exporter/CSV.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.0201_02';
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';
12  
13 1 has path => (is => 'ro');
14  
15 sub unit {
16 18 ▶ my ($self, $data) = @_;
Subroutine unit: called CC=2 SCAR=7.6
Pod undocumented
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 sub bind {
27 17 ▶ my ($self, $mvar, $func) = @_;
Subroutine bind: called CC=3 SCAR=13.3
Pod undocumented
28  
29 17 ▶ if (Catmandu::Util::is_array_ref($mvar)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($mvar)) { } 0 17 17
elsif (Ref::Util::XS::is_plain_hashref($mvar)) { } 17 0 17
30 0 return $self->bind_array($mvar, $func, '');
31 }
32 elsif (Catmandu::Util::is_hash_ref($mvar)) {
33 17 return $self->bind_hash($mvar, $func, '');
34 }
35 else {
36 0 return $self->bind_hash($mvar, $func, '');
37 }
38 }
39  
40 sub bind_scalar {
41 0 ▶ my ($self, $mvar, $func, $parent) = @_;
Subroutine bind_scalar: not called CC=1 SCAR=6.9
Pod undocumented
42  
43 0 return $func->({'key' => $parent, 'scalar' => $mvar})->{'scalar'};
44 }
45  
46 sub bind_array {
47 1 ▶ my ($self, $mvar, $func, $parent) = @_;
Subroutine bind_array: called CC=4 SCAR=14.5
Pod undocumented
48  
49 1 $mvar = $func->({'key' => $parent, 'array' => $mvar})->{'array'};
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 (Ref::Util::XS::is_plain_arrayref($item)) { } 0 2 2
elsif (Ref::Util::XS::is_plain_hashref($item)) { } 2 0 2
56 0 $mvar->[$i] = $self->bind_array($item, $func, $i);
57 }
58 elsif (Catmandu::Util::is_hash_ref($item)) {
59 2 $mvar->[$i] = $self->bind_hash($item, $func, $i);
60 }
61 else {
62 $mvar->[$i]
63 0 = $func->({'key' => $i, 'scalar' => $item})->{'scalar'};
64 }
65 }
66  
67 1 return $mvar;
68 }
69  
70 sub bind_hash {
71 24 ▶ my ($self, $mvar, $func, $parent) = @_;
Subroutine bind_hash: called CC=4 SCAR=13.9
Pod undocumented
72  
73 24 $mvar = $func->({'key' => $parent, 'hash' => $mvar})->{'hash'};
74  
75 24 for my $key (keys %$mvar) {
76 52 my $item = $mvar->{$key};
77  
78 52 ▶ if (Catmandu::Util::is_array_ref($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($item)) { } 1 51 52
elsif (Ref::Util::XS::is_plain_hashref($item)) { } 5 46 51
79 1 $mvar->{$key} = $self->bind_array($item, $func, $key);
80 }
81 elsif (Catmandu::Util::is_hash_ref($item)) {
82 5 $mvar->{$key} = $self->bind_hash($item, $func, $key);
83 }
84 else {
85 $mvar->{$key}
86 46 = $func->({'key' => $key, 'scalar' => $item})->{'scalar'};
87 }
88 }
89  
90 24 return $mvar;
91 }
92  
93 1 1;
94  
95 __END__
96  
97 =pod
98  
99 =head1 NAME
100  
101 Catmandu::Fix::Bind::visitor - a binder that computes Fix-es for every element in record
102  
103 =head1 SYNOPSIS
104  
105 # If data is like:
106
107 numbers:
108 - one
109 - two
110 - three
111 person:
112 name: jennie
113 age: 44
114 color:
115 - green
116 - purple
117 - brown
118 - more:
119 foo: bar
120  
121 do visitor()
122 upcase(scalar) # upcase every scalar value in the record
123 end
124  
125 # will produce
126  
127 numbers:
128 - ONE
129 - TWO
130 - THREE
131 person:
132 name: JENNIE
133 age: 44
134 color:
135 - GREEN
136 - PURPLE
137 - BROWN
138 - more:
139 foo: BAR
140  
141 do visitor()
142 # upcase all the 'name' fields in the record
143 if all_match(key,name)
144 upcase(scalar)
145 end
146 end
147  
148 =head1 DESCRIPTION
149  
150 The visitor binder will iterate over all the elements in a record and perform fixes on them.
151  
152 Special node names are available to process every visited element:
153  
154 =over 4
155  
156 =item scalar
157  
158 Process a Fix on every scalar value. E.g.
159  
160 upcase(scalar)
161 replace_all(scalar,'$','tested')
162  
163 =item array
164  
165 Process a Fix on every array value. E.g.
166  
167 sort_field(array)
168  
169 Values need to be put in the 'array' field to be available for fixes. The scope of
170 the array is limited to the array visited.
171  
172 =item hash
173  
174 Process a Fix on every hash value. E.g.
175  
176 copy_field(hash.age,hash.age2)
177  
178 Values need to be put in the 'hash' field to be available for fixes. The scope of
179 the hash is limited to the hash visited.
180  
181 =item key
182  
183 Provides access to the key on which the scalar,array or hash value is found. Eg.
184  
185 # Upcase all 'name' fields in the record
186 if all_match(key,name)
187 upcase(scalar)
188 end
189  
190 =back
191  
192 =head1 CONFIGURATION
193  
194 =head2 path
195  
196 A path in the data to visit:
197  
198 # Visit any field
199 do visitor()
200 ...
201 end
202  
203 # Visit only the fields at my.deep.field
204 do visitor(-path => my.deep.field )
205 ...
206 end
207  
208 =head1 SEE ALSO
209  
210 L<Catmandu::Fix::Bind>
211  
212 =cut
« blib/lib/Catmandu/Fix/Bind/with.pm ↑ Summary blib/lib/Catmandu/Exporter/CSV.pm »