blib/lib/Catmandu/Fix/lookup.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 98.5% 67 / 68 CC 4 SCAR 6.5
CC
4
Cov
100%
__ANON__
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.9
« blib/lib/Catmandu/Importer/CSV.pm ↑ Summary blib/lib/Catmandu/Fix/substring.pm »
1 package Catmandu::Fix::lookup;
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 Catmandu::Importer::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
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::Base';
13  
14 1 has path => (fix_arg => 1);
15 1 has file => (fix_arg => 1);
16 1 has default => (fix_opt => 1);
17 1 has delete => (fix_opt => 1);
18 1 has csv_args => (fix_opt => 'collect');
19 1 has dictionary => (is => 'lazy', init_arg => undef);
20  
21 sub _build_dictionary {
22 7 ▶ my ($self) = @_;
Subroutine _build_dictionary: called CC=1 SCAR=0.0
23 Catmandu::Importer::CSV->new(
24 7 %{$self->csv_args},
25 file => $self->file,
26 header => 0,
27 fields => ['key', 'val'],
28 )->reduce(
29 {},
30 sub {
31 36 ▶ my ($dict, $pair) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
32 36 $dict->{$pair->{key}} = $pair->{val};
33 36 $dict;
34 }
35 7 );
36 }
37  
38 sub emit {
39 7 ▶ my ($self, $fixer) = @_;
Subroutine emit: called CC=1 SCAR=0.0
Pod undocumented
40 7 my $path = $fixer->split_path($self->path);
41 7 my $key = pop @$path;
42 7 my $dict_var = $fixer->capture($self->dictionary);
43 7 my $delete = $self->delete;
44 7 my $default = $self->default;
45  
46 $fixer->emit_walk_path(
47 $fixer->var,
48 $path,
49 sub {
50 7 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
51 $fixer->emit_get_key(
52 $var, $key,
53 sub {
54 7 ▶ my $val_var = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
55 7 my $val_index = shift;
56 7 my $dict_val_var = $fixer->generate_var;
57 7 my $perl
58 = "if (is_value(${val_var}) && defined(my ${dict_val_var} = ${dict_var}->{${val_var}})) {"
59 . "${val_var} = ${dict_val_var};" . "}";
60 7 ▶ if ($delete) {
Branch: 2 branches
BranchTrueFalseTotal
if ($delete) { } 2 5 7
elsif (defined $default) { } 2 3 5
61 2 $perl .= "else {";
62 2 ▶ if (defined $val_index)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val_index) { } 1 1 2
63 { # wildcard: only delete the value where the lookup failed
64 1 $perl .= "splice(\@{${var}}, ${val_index}--, 1);";
65 }
66 else {
67 1 $perl .= $fixer->emit_delete_key($var, $key);
68 }
69 2 $perl .= "}";
70 }
71 elsif (defined $default) {
72 2 $perl
73 .= "else {"
74 . "${val_var} = "
75 . $fixer->emit_value($default) . ";" . "}";
76 }
77 7 $perl;
78 }
79 7 );
80 }
81 7 );
82 }
83  
84 1 1;
85  
86 __END__
87  
88 =pod
89  
90 =head1 NAME
91  
92 Catmandu::Fix::lookup - change the value of a HASH key or ARRAY index by
93 looking up its value in a dictionary
94  
95 =head1 SYNOPSIS
96  
97 lookup(foo.bar, dictionary.csv)
98 lookup(foo.bar, dictionary.csv, sep_char: |)
99 # delete value if the lookup fails:
100 lookup(foo.bar, dictionary.csv, delete: 1)
101 # use a default value if the lookup fails:
102 lookup(foo.bar, dictionary.csv, default: 'default value')
103  
104 =head1 SEE ALSO
105  
106 L<Catmandu::Fix>
107  
108 =cut
« blib/lib/Catmandu/Importer/CSV.pm ↑ Summary blib/lib/Catmandu/Fix/substring.pm »