blib/lib/Catmandu/Fix/assoc.pm

filter: Statementstmt 100% 40 / 40 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 98.0% 50 / 51 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Catmandu/Importer/Modules.pm ↑ Summary blib/lib/Catmandu/Droppable.pm »
1 package Catmandu::Fix::assoc;
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 namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has path => (fix_arg => 1);
12 1 has keys_path => (fix_arg => 1);
13 1 has vals_path => (fix_arg => 1);
14  
15 1 with 'Catmandu::Fix::Base';
16  
17 sub emit {
18 2 ▶ my ($self, $fixer) = @_;
Subroutine emit: called CC=1 SCAR=0.0
Pod undocumented
19 2 my $path = $fixer->split_path($self->path);
20 2 my $keys_path = $fixer->split_path($self->keys_path);
21 2 my $vals_path = $fixer->split_path($self->vals_path);
22 2 my $keys_key = pop @$keys_path;
23 2 my $vals_key = pop @$vals_path;
24  
25 2 my $keys_var = $fixer->generate_var;
26 2 my $vals_var = $fixer->generate_var;
27 2 my $perl
28 = $fixer->emit_declare_vars([$keys_var, $vals_var], ['[]', '[]']);
29  
30 $perl .= $fixer->emit_walk_path(
31 $fixer->var,
32 $keys_path,
33 sub {
34 2 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
35 $fixer->emit_get_key(
36 $var,
37 $keys_key,
38 sub {
39 2 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 "push(\@{${keys_var}}, ${var}) if is_value(${var});";
41 }
42 2 );
43 }
44 2 );
45 $perl .= "if (\@{${keys_var}}) {" . $fixer->emit_walk_path(
46 $fixer->var,
47 $vals_path,
48 sub {
49 2 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 $fixer->emit_get_key(
51 $var,
52 $vals_key,
53 sub {
54 2 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
55 2 "push(\@{${vals_var}}, ${var});";
56 }
57 2 );
58 }
59 )
60 . $fixer->emit_create_path(
61 $fixer->var,
62 $path,
63 sub {
64 2 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
65 2 "if (is_hash_ref(${var} //= {})) {"
66 . "while (\@{${keys_var}} && \@{${vals_var}}) {"
67 . "${var}\->{shift(\@{${keys_var}})} = shift(\@{${vals_var}});"
68 . "}" . "}";
69 }
70 2 ) . "}";
71  
72 2 $perl;
73 }
74  
75 1 1;
76  
77 __END__
78  
79 =pod
80  
81 =head1 NAME
82  
83 Catmandu::Fix::assoc - associate two values as a hash key and value
84  
85 =head1 SYNOPSIS
86  
87 # {pairs => [{key => 'year', val => 2009}, {key => 'subject', val => 'Perl'}]}
88 assoc(fields, pairs.*.key, pairs.*.val)
89 # {fields => {subject => 'Perl', year => 2009}, pairs => [...]}
90  
91 =head1 SEE ALSO
92  
93 L<Catmandu::Fix>
94  
95 =cut
« blib/lib/Catmandu/Importer/Modules.pm ↑ Summary blib/lib/Catmandu/Droppable.pm »