line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::Bind::list; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1125
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
9
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
390
|
use Clone (); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
9
|
1
|
|
|
1
|
|
7
|
use Catmandu::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
10
|
1
|
|
|
1
|
|
6
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
11
|
1
|
|
|
1
|
|
819
|
use Catmandu::Fix::Has; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Catmandu::Fix::Bind', 'Catmandu::Fix::Bind::Group'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has path => (fix_opt => 1); |
16
|
|
|
|
|
|
|
has var => (fix_opt => 1); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has root => (is => 'rw'); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub zero { |
21
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
22
|
0
|
|
|
|
|
0
|
[]; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub unit { |
26
|
13
|
|
|
13
|
0
|
31
|
my ($self, $data) = @_; |
27
|
|
|
|
|
|
|
|
28
|
13
|
|
|
|
|
39
|
$self->root($data); |
29
|
|
|
|
|
|
|
|
30
|
13
|
100
|
|
|
|
211
|
defined $self->path ? Catmandu::Util::data_at($self->path, $data) : $data; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub bind { |
34
|
|
|
|
|
|
|
my ($self, $mvar, $code) = @_; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $root = $self->root; |
37
|
|
|
|
|
|
|
my $var = $self->var; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
if (Catmandu::Util::is_hash_ref($mvar)) { |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Ignore all specialized processing when not an array |
42
|
|
|
|
|
|
|
$mvar = $code->($mvar); |
43
|
|
|
|
|
|
|
return $mvar; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif (Catmandu::Util::is_array_ref($mvar)) { |
46
|
|
|
|
|
|
|
for my $item (@$mvar) { |
47
|
|
|
|
|
|
|
if (defined $var) { |
48
|
|
|
|
|
|
|
$root->{$var} = $item; |
49
|
|
|
|
|
|
|
$root = $code->($root); |
50
|
|
|
|
|
|
|
delete $root->{$var}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
|
|
|
|
|
|
$item = $code->($item); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
return $mvar; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
|
|
|
|
|
|
return $self->zero; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
1; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
__END__ |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=pod |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 NAME |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Catmandu::Fix::Bind::list - a binder that computes Fix-es for every element in a list |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 SYNOPSIS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Create an array: |
76
|
|
|
|
|
|
|
# demo: |
77
|
|
|
|
|
|
|
# - red |
78
|
|
|
|
|
|
|
# - green |
79
|
|
|
|
|
|
|
# - yellow |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Add a foo field to every item in the demo list, by default all |
82
|
|
|
|
|
|
|
# fixes will be in context of the iterated path. If the context |
83
|
|
|
|
|
|
|
# is a list, then '.' will be the path of the temporary context |
84
|
|
|
|
|
|
|
# variable |
85
|
|
|
|
|
|
|
do list(path:demo) |
86
|
|
|
|
|
|
|
if all_equal(.,green) |
87
|
|
|
|
|
|
|
upcase(.) |
88
|
|
|
|
|
|
|
end |
89
|
|
|
|
|
|
|
end |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# This will result: |
92
|
|
|
|
|
|
|
# demo: |
93
|
|
|
|
|
|
|
# - red |
94
|
|
|
|
|
|
|
# - GREEN |
95
|
|
|
|
|
|
|
# - yellow |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# Loop over the list but store the values in a temporary 'c' variable |
98
|
|
|
|
|
|
|
# Use this c variable to copy the list to the root 'xyz' path |
99
|
|
|
|
|
|
|
do list(path:demo,var:c) |
100
|
|
|
|
|
|
|
copy_field(c,xyz.$append) |
101
|
|
|
|
|
|
|
end |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# This will result: |
104
|
|
|
|
|
|
|
# demo: |
105
|
|
|
|
|
|
|
# - red |
106
|
|
|
|
|
|
|
# - GREEN |
107
|
|
|
|
|
|
|
# - yellow |
108
|
|
|
|
|
|
|
# xyz: |
109
|
|
|
|
|
|
|
# - red |
110
|
|
|
|
|
|
|
# - GREEN |
111
|
|
|
|
|
|
|
# - yellow |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 DESCRIPTION |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
The list binder will iterate over all the elements in a list and fixes the |
116
|
|
|
|
|
|
|
values in context of that list. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 CONFIGURATION |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 path |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
The path to a list in the data. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head2 var |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The loop variable to be iterated over. When used, a magic temporary field will |
127
|
|
|
|
|
|
|
be available in the root of the record containing the iterated data. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SEE ALSO |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
L<Catmandu::Fix::Bind> |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |